diff --git a/database.db b/database.db index abd3ec3..22f568f 100644 Binary files a/database.db and b/database.db differ diff --git a/fleetcontrol b/fleetcontrol index a4fe5b1..a3ebb26 100644 --- a/fleetcontrol +++ b/fleetcontrol @@ -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] -server.run() +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 diff --git a/fleetcontrol-settings-manager b/fleetcontrol-settings-manager index eb824eb..fa47f74 100644 --- a/fleetcontrol-settings-manager +++ b/fleetcontrol-settings-manager @@ -1,6 +1,9 @@ #!/usr/bin/python3 from utils.database.database import Database +from utils.config.config import ServerConfig + + # vi: ft=python diff --git a/network/__pycache__/__init__.cpython-310.pyc b/network/__pycache__/__init__.cpython-310.pyc index e2cac36..c9fda49 100644 Binary files a/network/__pycache__/__init__.cpython-310.pyc and b/network/__pycache__/__init__.cpython-310.pyc differ diff --git a/network/__pycache__/communication.cpython-310.pyc b/network/__pycache__/communication.cpython-310.pyc index ce0e29d..fab1572 100644 Binary files a/network/__pycache__/communication.cpython-310.pyc and b/network/__pycache__/communication.cpython-310.pyc differ diff --git a/temp_file.qcow2 b/temp_file.qcow2 new file mode 100644 index 0000000..e69de29 diff --git a/utils/__pycache__/__init__.cpython-310.pyc b/utils/__pycache__/__init__.cpython-310.pyc index 0b11feb..e02e508 100644 Binary files a/utils/__pycache__/__init__.cpython-310.pyc and b/utils/__pycache__/__init__.cpython-310.pyc differ diff --git a/utils/config/__pycache__/__init__.cpython-310.pyc b/utils/config/__pycache__/__init__.cpython-310.pyc index b1984a5..18c9788 100644 Binary files a/utils/config/__pycache__/__init__.cpython-310.pyc and b/utils/config/__pycache__/__init__.cpython-310.pyc differ diff --git a/utils/config/__pycache__/config.cpython-310.pyc b/utils/config/__pycache__/config.cpython-310.pyc index a88902d..83d02c0 100644 Binary files a/utils/config/__pycache__/config.cpython-310.pyc and b/utils/config/__pycache__/config.cpython-310.pyc differ diff --git a/utils/database/__pycache__/__init__.cpython-310.pyc b/utils/database/__pycache__/__init__.cpython-310.pyc index 7ad363e..6243c85 100644 Binary files a/utils/database/__pycache__/__init__.cpython-310.pyc and b/utils/database/__pycache__/__init__.cpython-310.pyc differ diff --git a/utils/database/__pycache__/database.cpython-310.pyc b/utils/database/__pycache__/database.cpython-310.pyc index fabbb5b..b437982 100644 Binary files a/utils/database/__pycache__/database.cpython-310.pyc and b/utils/database/__pycache__/database.cpython-310.pyc differ diff --git a/utils/database/database.py b/utils/database/database.py index 0c196bb..42de3ce 100644 --- a/utils/database/database.py +++ b/utils/database/database.py @@ -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 @@ -142,6 +142,17 @@ class Database: except Exception as ex: 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: @@ -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() diff --git a/utils/exceptions/__pycache__/DatabaseException.cpython-310.pyc b/utils/exceptions/__pycache__/DatabaseException.cpython-310.pyc index 0e1edd7..a9a1ec8 100644 Binary files a/utils/exceptions/__pycache__/DatabaseException.cpython-310.pyc and b/utils/exceptions/__pycache__/DatabaseException.cpython-310.pyc differ diff --git a/utils/exceptions/__pycache__/__init__.cpython-310.pyc b/utils/exceptions/__pycache__/__init__.cpython-310.pyc index 8314339..2fe08db 100644 Binary files a/utils/exceptions/__pycache__/__init__.cpython-310.pyc and b/utils/exceptions/__pycache__/__init__.cpython-310.pyc differ diff --git a/utils/middleware/__pycache__/__init__.cpython-310.pyc b/utils/middleware/__pycache__/__init__.cpython-310.pyc index 8746ad3..d4b20c7 100644 Binary files a/utils/middleware/__pycache__/__init__.cpython-310.pyc and b/utils/middleware/__pycache__/__init__.cpython-310.pyc differ diff --git a/utils/middleware/__pycache__/auth.cpython-310.pyc b/utils/middleware/__pycache__/auth.cpython-310.pyc index 97c9706..1270f95 100644 Binary files a/utils/middleware/__pycache__/auth.cpython-310.pyc and b/utils/middleware/__pycache__/auth.cpython-310.pyc differ diff --git a/utils/models/__pycache__/__init__.cpython-310.pyc b/utils/models/__pycache__/__init__.cpython-310.pyc index 1044dea..410b9a7 100644 Binary files a/utils/models/__pycache__/__init__.cpython-310.pyc and b/utils/models/__pycache__/__init__.cpython-310.pyc differ diff --git a/utils/models/__pycache__/models.cpython-310.pyc b/utils/models/__pycache__/models.cpython-310.pyc index 7e2264e..9c4cd13 100644 Binary files a/utils/models/__pycache__/models.cpython-310.pyc and b/utils/models/__pycache__/models.cpython-310.pyc differ diff --git a/utils/tools/__init__.py b/utils/tools/__init__.py new file mode 100644 index 0000000..fed9bd2 --- /dev/null +++ b/utils/tools/__init__.py @@ -0,0 +1 @@ +from . import tools \ No newline at end of file diff --git a/utils/tools/__pycache__/__init__.cpython-310.pyc b/utils/tools/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..b770eaa Binary files /dev/null and b/utils/tools/__pycache__/__init__.cpython-310.pyc differ diff --git a/utils/tools/__pycache__/tools.cpython-310.pyc b/utils/tools/__pycache__/tools.cpython-310.pyc new file mode 100644 index 0000000..4305466 Binary files /dev/null and b/utils/tools/__pycache__/tools.cpython-310.pyc differ diff --git a/utils/tools/tools.py b/utils/tools/tools.py new file mode 100644 index 0000000..e438c4b --- /dev/null +++ b/utils/tools/tools.py @@ -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() \ No newline at end of file