master
Wojciech Janota 2 years ago
parent 2662f0cf4e
commit 9075f4c538

Binary file not shown.

@ -1,11 +1,110 @@
#!/usr/bin/python3
from network.communication import Server
from utils.database.database import Database
from utils.config.config import ServerConfig
from utils.models.models import VMImage
from utils.tools.tools import md5
import logging
import argparse
config = ServerConfig()
server = Server(host=config.server_host, port=config.server_port, name=config.server_name, access_password=config.server_password,
access_username=config.server_access_username, jwt_secret=config.jwt_secret, version="v0.0.1alpha", database_file_path=config.database_file, logging_level=config.server_loglevel)
logger = logging.getLogger(__name__)
log_level_mapping_dict = {
"NOTSET": 0,
"DEBUG": 10,
"INFO": 20,
"WARNING": 30,
"ERROR": 40,
"CRITICAL": 50,
}
logger.setLevel(log_level_mapping_dict[config.server_loglevel])
logger = logging.getLogger()
logger.setLevel
def run_server():
server = Server(
host=config.server_host,
port=config.server_port,
name=config.server_name,
access_password=config.server_password,
access_username=config.server_access_username,
jwt_secret=config.jwt_secret,
version="v0.0.1alpha",
database_file_path=config.database_file,
logging_level=config.server_loglevel,
)
logger.info(
f"Running server on host: {config.server_host}, port: {config.server_port}, server name: {config.server_name}"
)
server.run()
def add_image(image_name: str, image_file: str, image_version: str):
try:
new_image_hash = md5(image_file)
new_image_object = VMImage(
image_name=image_name,
image_file=image_file,
image_version=image_version,
image_hash=new_image_hash,
image_name_version_combo=f"{image_name}@{image_version}",
)
db = Database(config.database_file, config.server_loglevel)
db.add_image(new_image_object)
except Exception as ex:
logger.error(f"Error adding image to the database: {str(ex)}")
exit(-1)
def assign_image(image_name: str, image_version: str, client_mac_address: str):
try:
db = Database(config.database_file, config.server_loglevel)
db.assign_image_to_client(client_mac_address=client_mac_address, image_name_version_combo=f"{image_name}@{image_version}")
except Exception as ex:
logger.error(f"Error assigning image to a client: {str(ex)}")
parser = argparse.ArgumentParser(
prog="fleetcontrol",
description="Server and configuration utility for orchestrating VM machine clients",
)
function_mapper = {
"run": run_server,
"add_image": add_image,
"assign_image": assign_image,
}
parser.add_argument("command", choices=function_mapper)
parser.add_argument("--image-name", action="store")
parser.add_argument("--image-filepath", action="store")
parser.add_argument("--image-version", action="store")
parser.add_argument("--mac-address", action="store")
args = parser.parse_args()
fun = function_mapper[args.command]
if "add_image" == args.command:
fun(
image_name=args.image_name,
image_file=args.image_filepath,
image_version=args.image_version,
)
elif "assign_image" == args.command:
fun(
image_name=args.image_name,
image_version=args.image_version,
client_mac_address=args.mac_address,
)
elif "run" == args.command:
fun()
else:
logger.error(f"Invalid operand: {args.command}")
exit(-1)
# vi: ft=python

@ -1,6 +1,9 @@
#!/usr/bin/python3
from utils.database.database import Database
from utils.config.config import ServerConfig
# vi: ft=python

@ -49,7 +49,7 @@ class Database:
try:
with session.begin():
result = session.query(
Client, mac_address=mac_address).first()
Client).filter(Client.mac_address==mac_address).first()
except Exception as ex:
self.logger.warn(f"Error getting client by mac address: {ex}")
return result
@ -143,6 +143,17 @@ class Database:
self.logger.error(
f"Error getting list of images from database: {ex}")
def get_image_by_name_version_string(self, image_name_version_string: str) -> list[VMImage]:
try:
session = self.Session()
with session.begin():
response = session.query(
VMImage).filter(VMImage.image_name_version_combo==image_name_version_string).first()
return response
except Exception as ex:
self.logger.error(
f"Error getting list of images from database: {ex}")
def get_image_by_hash(self, image_hash: str) -> list[VMImage]:
try:
session = self.Session()
@ -179,6 +190,20 @@ class Database:
raise DatabaseException(
f"Couldn't modify object in database: {ex}")
def assign_image_to_client(self, client_mac_address: str, image_name_version_combo: str):
try:
session = self.Session()
with session.begin():
client = session.query(Client).filter(Client.mac_address==client_mac_address).first()
image = session.query(VMImage).filter(VMImage.image_name_version_combo==image_name_version_combo).first()
client.vm_list_on_machine.append(image)
session.merge(client)
session.flush()
session.commit()
except Exception as ex:
self.logger.error(f"Couldn't add image to client list: {str(ex)}")
raise DatabaseException(f"Couldn't add image to client list: {str(ex)}")
def add_user(self, new_user: User):
try:
session = self.Session()

@ -0,0 +1 @@
from . import tools

@ -0,0 +1,9 @@
import hashlib
# from https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
Loading…
Cancel
Save