WIP
This commit is contained in:
parent
3b540e4b26
commit
d2066d45b2
@ -1,2 +1,3 @@
|
|||||||
server_name: "valhalla"
|
server_name: "valhalla"
|
||||||
server_port: 8088
|
server_port: 8088
|
||||||
|
logging_level: "INFO"
|
||||||
|
@ -1,14 +1,28 @@
|
|||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from utils.models import Client, VMImage
|
from utils.models import Client, VMImage
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self, database_file: str):
|
def __init__(self, database_file: str, logging_level: str):
|
||||||
try:
|
try:
|
||||||
# Connect to the database using SQLAlchemy
|
# Connect to the database using SQLAlchemy
|
||||||
engine = create_engine(f"sqlite:///{database_file}")
|
engine = create_engine(f"sqlite:///{database_file}")
|
||||||
Session = sessionmaker()
|
Session = sessionmaker()
|
||||||
Session.configure(bind=engine)
|
Session.configure(bind=engine)
|
||||||
self.session = Session()
|
self.session = Session()
|
||||||
|
# create logger using data from config file
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
log_level_mapping_dict = {
|
||||||
|
"NOTSET": 0,
|
||||||
|
"DEBUG": 10,
|
||||||
|
"INFO": 20,
|
||||||
|
"WARNING": 30,
|
||||||
|
"ERROR": 40,
|
||||||
|
"CRITICAL": 50
|
||||||
|
}
|
||||||
|
self.logger.setLevel(log_level_mapping_dict[logging_level])
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print(ex)
|
print(ex)
|
||||||
exit(-1)
|
exit(-1)
|
||||||
@ -16,12 +30,23 @@ class Database:
|
|||||||
def get_clients(self) -> list(Client):
|
def get_clients(self) -> list(Client):
|
||||||
result = None
|
result = None
|
||||||
try:
|
try:
|
||||||
result = self.session.query(Client).all()
|
with self.session.begin():
|
||||||
|
result = self.session.query(Client).all()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print(f"Error getting list of clients from database: {ex}")
|
self.logger.error(
|
||||||
|
f"Error getting list of clients from database: {ex}")
|
||||||
result = None
|
result = None
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def get_client_by_mac_address(mac_address: str) -> Client:
|
def get_client_by_mac_address(self, mac_address: str) -> Client:
|
||||||
result = None
|
result = None
|
||||||
|
try:
|
||||||
|
with self.session.begin():
|
||||||
|
result = self.session.query(
|
||||||
|
Client, mac_address=mac_address).first()
|
||||||
|
except Exception as ex:
|
||||||
|
self.logger.error(f"Error getting client by mac address: {ex}")
|
||||||
|
return result
|
||||||
|
|
||||||
|
def get_clients_by_client_version(self, client_version: str) -> list(Client):
|
||||||
|
result = None
|
||||||
|
Loading…
Reference in New Issue
Block a user