Compare commits
3
Commits
98e9b28bae
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
087fed017a | ||
|
|
220a06208a | ||
|
|
d268cb01d5 |
@@ -3,6 +3,7 @@ import logging
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from flags import status
|
||||||
|
|
||||||
|
|
||||||
print("loading")
|
print("loading")
|
||||||
@@ -129,7 +130,6 @@ def exit_programm():
|
|||||||
print("")
|
print("")
|
||||||
print("Disconnect DB")
|
print("Disconnect DB")
|
||||||
print("logout")
|
print("logout")
|
||||||
os.remove("migration_status.flag")
|
|
||||||
print("shutting down")
|
print("shutting down")
|
||||||
time.sleep(0.75)
|
time.sleep(0.75)
|
||||||
os.system('cls' if os.name == 'nt' else 'clear')
|
os.system('cls' if os.name == 'nt' else 'clear')
|
||||||
@@ -178,7 +178,7 @@ def menu():
|
|||||||
|
|
||||||
|
|
||||||
#The Terminal App Starts here
|
#The Terminal App Starts here
|
||||||
time.sleep(5)
|
time.sleep(2)
|
||||||
print(r"""
|
print(r"""
|
||||||
## ## ##########
|
## ## ##########
|
||||||
### ### ## ##
|
### ### ## ##
|
||||||
@@ -187,8 +187,8 @@ print(r"""
|
|||||||
## ## ## ## ##
|
## ## ## ## ##
|
||||||
## ## ## ##
|
## ## ## ##
|
||||||
""")
|
""")
|
||||||
|
while not status.get_migration():
|
||||||
|
time.sleep(0.5)
|
||||||
logging.info("Menu is starting")
|
logging.info("Menu is starting")
|
||||||
time.sleep(2.5)
|
time.sleep(2.5)
|
||||||
while not os.path.exists("migration_status.flag"):
|
|
||||||
time.sleep(0.5)
|
|
||||||
menu()
|
menu()
|
||||||
|
|||||||
@@ -95,6 +95,7 @@ def fetch_all_pieces():
|
|||||||
result = session.query(Items.pieces).all()
|
result = session.query(Items.pieces).all()
|
||||||
return [row[0] for row in 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):
|
def check_clothe(clothe_type):
|
||||||
exists = session.query(Items.clothes).filter_by(clothes=clothe_type).first() is not None
|
exists = session.query(Items.clothes).filter_by(clothes=clothe_type).first() is not None
|
||||||
return exists
|
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 auth.api_security as auth
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
|
from tests import db_test
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.remove("api.log")
|
os.remove("api.log")
|
||||||
@@ -43,24 +44,39 @@ def run_migration():
|
|||||||
)
|
)
|
||||||
process.wait()
|
process.wait()
|
||||||
migration_done = (process.returncode == 0)
|
migration_done = (process.returncode == 0)
|
||||||
with open("migration_status.flag", "w") as f:
|
with open("flags/migration_status.flag", "w") as f:
|
||||||
f.write("done" if migration_done else "failed")
|
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
|
#cli and api
|
||||||
log_file = open("api_server.log", "w", encoding="utf-8")
|
log_file = open("api_server.log", "w", encoding="utf-8")
|
||||||
api_process = subprocess.Popen(
|
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,
|
stdout=log_file,
|
||||||
stderr=log_file,
|
stderr=log_file,
|
||||||
env=env
|
env=env
|
||||||
)
|
)
|
||||||
cli_process = subprocess.Popen([sys.executable, "cli.py"])
|
cli_process = subprocess.Popen([sys.executable, "cli.py"])
|
||||||
|
|
||||||
api_process.wait()
|
db_thread = threading.Thread(target=run_db_test)
|
||||||
cli_process.wait()
|
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
|
# automatic durataion validation check
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
@@ -6,3 +6,5 @@ MarkupSafe==3.0.3
|
|||||||
itsdangerous==2.2.0
|
itsdangerous==2.2.0
|
||||||
alembic==1.20.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
|
from migration.alembic_scipt import auto_lookup
|
||||||
import db
|
import time
|
||||||
import os
|
|
||||||
import logging
|
import logging
|
||||||
import auth.api_security as auth
|
import auth.api_security as auth
|
||||||
|
import os
|
||||||
|
|
||||||
#Logging
|
#Logging
|
||||||
|
|
||||||
@@ -18,41 +18,19 @@ try:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception('An error occurred')
|
logging.exception('An error occurred')
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
db_name = os.getenv("DB_NAME")
|
def new_table():
|
||||||
db_psswd = os.getenv("DB_PASSWORD")
|
auto_lookup()
|
||||||
db_user = os.getenv("DB_USER")
|
|
||||||
db_port = os.getenv("DB_PORT") or "3306"
|
def admin():
|
||||||
db_host = os.getenv("DB_HOST")
|
token = auth.admin_token()
|
||||||
if db_host == None:
|
print(token)
|
||||||
logging.error("no hostname provided")
|
logging.info(token)
|
||||||
|
time.sleep(300)
|
||||||
|
os.remove("app.log")
|
||||||
os._exit(1)
|
os._exit(1)
|
||||||
|
|
||||||
db.connect(db_user, db_psswd, db_host, int(db_port), db_name)
|
|
||||||
|
|
||||||
def new_table(name):
|
new_table()
|
||||||
cursor = db.connection.cursor()
|
admin()
|
||||||
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())
|
|
||||||
|
|
||||||
|
|||||||
@@ -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