Compare commits
3
Commits
98e9b28bae
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
087fed017a | ||
|
|
220a06208a | ||
|
|
d268cb01d5 |
@@ -3,6 +3,7 @@ import logging
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
from flags import status
|
||||
|
||||
|
||||
print("loading")
|
||||
@@ -129,7 +130,6 @@ def exit_programm():
|
||||
print("")
|
||||
print("Disconnect DB")
|
||||
print("logout")
|
||||
os.remove("migration_status.flag")
|
||||
print("shutting down")
|
||||
time.sleep(0.75)
|
||||
os.system('cls' if os.name == 'nt' else 'clear')
|
||||
@@ -178,7 +178,7 @@ def menu():
|
||||
|
||||
|
||||
#The Terminal App Starts here
|
||||
time.sleep(5)
|
||||
time.sleep(2)
|
||||
print(r"""
|
||||
## ## ##########
|
||||
### ### ## ##
|
||||
@@ -187,8 +187,8 @@ print(r"""
|
||||
## ## ## ## ##
|
||||
## ## ## ##
|
||||
""")
|
||||
while not status.get_migration():
|
||||
time.sleep(0.5)
|
||||
logging.info("Menu is starting")
|
||||
time.sleep(2.5)
|
||||
while not os.path.exists("migration_status.flag"):
|
||||
time.sleep(0.5)
|
||||
menu()
|
||||
|
||||
@@ -95,6 +95,7 @@ def fetch_all_pieces():
|
||||
result = session.query(Items.pieces).all()
|
||||
return [row[0] for row in session.query(Items.pieces).all()]
|
||||
|
||||
#Checks if clothe is in db
|
||||
def check_clothe(clothe_type):
|
||||
exists = session.query(Items.clothes).filter_by(clothes=clothe_type).first() is not None
|
||||
return exists
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import threading
|
||||
from watchdog.events import PatternMatchingEventHandler
|
||||
from watchdog.observers import Observer
|
||||
import os
|
||||
import time
|
||||
|
||||
FLAGS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
|
||||
|
||||
class FlagHandler(PatternMatchingEventHandler):
|
||||
def __init__(self, shared_state, lock):
|
||||
super().__init__(patterns=["*.flag"], ignore_directories=True)
|
||||
self.shared_state = shared_state
|
||||
self.lock = lock
|
||||
|
||||
def on_created(self, event):
|
||||
try:
|
||||
with open(event.src_path, "r") as f:
|
||||
content = f.read().strip()
|
||||
except FileNotFoundError:
|
||||
return
|
||||
try:
|
||||
os.remove(event.src_path)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
with self.lock:
|
||||
self.shared_state["current"] = content
|
||||
time.sleep(2)
|
||||
|
||||
shared_state = {"current": None}
|
||||
lock = threading.Lock()
|
||||
|
||||
observer = Observer()
|
||||
observer.schedule(FlagHandler(shared_state, lock), path=FLAGS_DIR, recursive=False)
|
||||
observer.start()
|
||||
|
||||
def get_status():
|
||||
with lock:
|
||||
current = shared_state["current"]
|
||||
if current == "db_check_done":
|
||||
return False
|
||||
return current
|
||||
|
||||
def get_migration():
|
||||
with lock:
|
||||
current = shared_state["current"]
|
||||
if current == "migration_done":
|
||||
return True
|
||||
return False
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
import auth.api_security as auth
|
||||
import time
|
||||
import threading
|
||||
from tests import db_test
|
||||
|
||||
try:
|
||||
os.remove("api.log")
|
||||
@@ -43,24 +44,39 @@ def run_migration():
|
||||
)
|
||||
process.wait()
|
||||
migration_done = (process.returncode == 0)
|
||||
with open("migration_status.flag", "w") as f:
|
||||
f.write("done" if migration_done else "failed")
|
||||
with open("flags/migration_status.flag", "w") as f:
|
||||
f.write("migration_done" if migration_done else "failed")
|
||||
|
||||
thread = threading.Thread(target=run_migration)
|
||||
thread.start()
|
||||
|
||||
|
||||
#DB test
|
||||
def run_db_test():
|
||||
tokens = db_test.test_table("tokens", ["tokens", "time", "end_date"])
|
||||
user = db_test.test_table("user", ["username", "password", "role"])
|
||||
items = db_test.test_table("luggage", ["clothes", "weight", "pieces"])
|
||||
time.sleep(5)
|
||||
if tokens == True & user == True & items == True:
|
||||
with open("flags/db.flag", "w") as f:
|
||||
f.write("db_check_done")
|
||||
else:
|
||||
os._exit(1)
|
||||
|
||||
#cli and api
|
||||
log_file = open("api_server.log", "w", encoding="utf-8")
|
||||
api_process = subprocess.Popen(
|
||||
["fastapi", "dev", "api_server.py"],
|
||||
["fastapi", "dev", "api_server.py", "--reload-dir", "web", "--reload-dir", "api", "--reload-dir", "api_server.py",],
|
||||
stdout=log_file,
|
||||
stderr=log_file,
|
||||
env=env
|
||||
)
|
||||
cli_process = subprocess.Popen([sys.executable, "cli.py"])
|
||||
|
||||
api_process.wait()
|
||||
cli_process.wait()
|
||||
db_thread = threading.Thread(target=run_db_test)
|
||||
db_thread.start()
|
||||
thread = threading.Thread(target=run_migration)
|
||||
thread.start()
|
||||
threading.Thread(target=api_process.wait, daemon=True).start()
|
||||
threading.Thread(target=cli_process.wait, daemon=True).start()
|
||||
|
||||
# automatic durataion validation check
|
||||
while True:
|
||||
|
||||
+3
-1
@@ -5,4 +5,6 @@ Jinja2==3.1.6
|
||||
MarkupSafe==3.0.3
|
||||
itsdangerous==2.2.0
|
||||
alembic==1.20.0
|
||||
SQLAlchemy==2.0.54
|
||||
SQLAlchemy==2.0.54
|
||||
watchdog==6.0.0
|
||||
#watchfiles==1.2.0
|
||||
@@ -1,8 +1,8 @@
|
||||
from dotenv import load_dotenv
|
||||
import db
|
||||
import os
|
||||
from migration.alembic_scipt import auto_lookup
|
||||
import time
|
||||
import logging
|
||||
import auth.api_security as auth
|
||||
import os
|
||||
|
||||
#Logging
|
||||
|
||||
@@ -18,41 +18,19 @@ try:
|
||||
except Exception as e:
|
||||
logging.exception('An error occurred')
|
||||
|
||||
load_dotenv()
|
||||
db_name = os.getenv("DB_NAME")
|
||||
db_psswd = os.getenv("DB_PASSWORD")
|
||||
db_user = os.getenv("DB_USER")
|
||||
db_port = os.getenv("DB_PORT") or "3306"
|
||||
db_host = os.getenv("DB_HOST")
|
||||
if db_host == None:
|
||||
logging.error("no hostname provided")
|
||||
|
||||
def new_table():
|
||||
auto_lookup()
|
||||
|
||||
def admin():
|
||||
token = auth.admin_token()
|
||||
print(token)
|
||||
logging.info(token)
|
||||
time.sleep(300)
|
||||
os.remove("app.log")
|
||||
os._exit(1)
|
||||
|
||||
db.connect(db_user, db_psswd, db_host, int(db_port), db_name)
|
||||
|
||||
def new_table(name):
|
||||
cursor = db.connection.cursor()
|
||||
if name == "tokens":
|
||||
executed = "CREATE TABLE tokens (tokens VARCHAR(255) PRIMARY KEY, `time` VARCHAR(255), end_date VARCHAR(255))"
|
||||
cursor.execute(executed)
|
||||
db.connection.commit()
|
||||
logging.info("Table 'tokens' created")
|
||||
|
||||
if name == "luggage":
|
||||
executed = "CREATE TABLE luggage (clothes VARCHAR(50) PRIMARY KEY, weight DECIMAL(20,2), pieces INT(11))"
|
||||
cursor.execute(executed)
|
||||
db.connection.commit()
|
||||
logging.info("Table 'luggage' created")
|
||||
|
||||
if name == "user":
|
||||
executed = "CREATE TABLE user (username VARCHAR(50) PRIMARY KEY, password VARCHAR(255))"
|
||||
cursor.execute(executed)
|
||||
db.connection.commit()
|
||||
logging.info("Table 'user' created")
|
||||
|
||||
|
||||
new_table("tokens")
|
||||
new_table("luggage")
|
||||
new_table("user")
|
||||
print(auth.admin_token())
|
||||
new_table()
|
||||
admin()
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
|
||||
|
||||
import os
|
||||
import db
|
||||
import logging
|
||||
import sys
|
||||
from migration import alembic_scipt as alembic
|
||||
from sqlalchemy import inspect
|
||||
|
||||
session = db.session
|
||||
engine = db.engine
|
||||
|
||||
def test_column(engine, tablename, row):
|
||||
try:
|
||||
inspector = inspect(engine)
|
||||
columns = [col["name"] for col in inspector.get_columns(tablename)]
|
||||
return row in columns
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
|
||||
|
||||
def test_table(table, columns):
|
||||
for column in columns:
|
||||
try:
|
||||
exists = test_column(engine, table, column)
|
||||
if exists == False:
|
||||
logging.error(f"DB has not all columns. {column} is missing")
|
||||
print(f"Column {column} is missing")
|
||||
sys.exit()
|
||||
os._exit(1)
|
||||
return False
|
||||
else:
|
||||
logging.info("Column fine")
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return True
|
||||
|
||||
def migration_check():
|
||||
if alembic.check_db() == False:
|
||||
print("migrating")
|
||||
logging.info("DB is migrating")
|
||||
Reference in New Issue
Block a user