From 9075f4c53870b2b0c0e3bb60b372b5e1bc963270 Mon Sep 17 00:00:00 2001 From: Wojciech Janota Date: Fri, 25 Nov 2022 14:12:08 +0100 Subject: [PATCH] WIP --- database.db | Bin 28672 -> 28672 bytes fleetcontrol | 105 +++++++++++++++++- fleetcontrol-settings-manager | 3 + network/__pycache__/__init__.cpython-310.pyc | Bin 208 -> 215 bytes .../__pycache__/communication.cpython-310.pyc | Bin 5604 -> 5965 bytes temp_file.qcow2 | 0 utils/__pycache__/__init__.cpython-310.pyc | Bin 185 -> 192 bytes .../__pycache__/__init__.cpython-310.pyc | Bin 206 -> 213 bytes .../config/__pycache__/config.cpython-310.pyc | Bin 1607 -> 1614 bytes .../__pycache__/__init__.cpython-310.pyc | Bin 210 -> 217 bytes .../__pycache__/database.cpython-310.pyc | Bin 7648 -> 8728 bytes utils/database/database.py | 27 ++++- .../DatabaseException.cpython-310.pyc | Bin 362 -> 369 bytes .../__pycache__/__init__.cpython-310.pyc | Bin 221 -> 228 bytes .../__pycache__/__init__.cpython-310.pyc | Bin 208 -> 215 bytes .../__pycache__/auth.cpython-310.pyc | Bin 1421 -> 1428 bytes .../__pycache__/__init__.cpython-310.pyc | Bin 206 -> 213 bytes .../models/__pycache__/models.cpython-310.pyc | Bin 2708 -> 2715 bytes utils/tools/__init__.py | 1 + .../__pycache__/__init__.cpython-310.pyc | Bin 0 -> 211 bytes utils/tools/__pycache__/tools.cpython-310.pyc | Bin 0 -> 593 bytes utils/tools/tools.py | 9 ++ 22 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 temp_file.qcow2 create mode 100644 utils/tools/__init__.py create mode 100644 utils/tools/__pycache__/__init__.cpython-310.pyc create mode 100644 utils/tools/__pycache__/tools.cpython-310.pyc create mode 100644 utils/tools/tools.py diff --git a/database.db b/database.db index abd3ec393d0da67500f54cd22c51ffa4cf5372b1..22f568f094d3feda1edbb2b3ca0865385254553d 100644 GIT binary patch delta 388 zcmZp8z}WDBae_1>_e2?IR&EBpWQL6?3-ozd_=*_#m+|}X-RCRXEGUq~SD($o&Y-QW z?CYGIpP!?fnVXoNs+*UXn_7~ZTM(a?nUktln4Dj3RA!)Opl6hrQ;?CEVq%zLk(^>_ zk!E0!WMp8HYH49$U}$3z)tRt^Tec$tkU3-mdd_CJ))`}tXD0+o|;siASlRg&R 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 e2cac369703d481705bec062333aae9aab125687..c9fda498f8473fdcadfd0e61f135f1accf7f5998 100644 GIT binary patch delta 52 zcmcb>c%6|upO=@50SNe;>n3t1$%g1>e*3ZkVNX^p^D9X=DO)e>(*yRfVA0!T8 diff --git a/network/__pycache__/communication.cpython-310.pyc b/network/__pycache__/communication.cpython-310.pyc index ce0e29dd6ff4a89983bc43eed0317be18a7d750f..fab1572b98270b1aa95e677e0d147c9b9c88c46c 100644 GIT binary patch delta 456 zcmZY5O-lkn7zgl~eQU8@MO{myA_@YDT*^Q?6&Peu5=e=bh`8fhrZH|=qINL4bZbsR z(Ji`)pQ1xwAUcH~qeJsVw+7}nf0%im2WGVN9&*-o-GTUW^}6}**@4q5%%sdx{fwBk z@)fC>2ee)xdAngo>(weLl3_%c7cpgN`BbweZ&6ya{0S5c4BzDFIR0R|`<~l(KeJDl z=2UoRXdmUfQDU3=#LcRWDq=@$iAiLk1d@;~U9Vu&kuYlcY}pbmWGii{hb`$*?BR}V z$!$41)|QheUw@LZf*Ih$nj)iSBsS)Ox!e%qMiXjsJx79KqQ>F-t`LFgr+ zq5toP5lQk!IIv(iusWr= Q*q34QdXI07yX|}W0agKOyZ`_I delta 153 zcmX@B_e7g7pO=@50SGqiEl<8Dvyo4jNi0Y|v^ce>SU)ebA~jDxpeR2pHMyjCvmKKX zGh^1|MAmGUU yXYv(MW1tAHn3|Xn&?XKBJthu*4rV5EE=DF1E=C@y$=+i6K#kR6CO~q7*jxacA1P7* 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 0b11feb9620c881ea5014a8a41ceb2e2a4bdb564..e02e5082209cc2aaec29574473c465c2a3e767c6 100644 GIT binary patch delta 52 zcmdnVcz}^RpO=@50SNe;>n3vh$_D9Yn3tX%SP&Fe*3ZkVNX^p^D9X=DO)e>(*c=1^Ad3!c diff --git a/utils/config/__pycache__/config.cpython-310.pyc b/utils/config/__pycache__/config.cpython-310.pyc index a88902dc8c0361409d5631605b50be03593708b0..83d02c0159dc0ffae8a57ccc53637c4729446a61 100644 GIT binary patch delta 55 zcmX@kbB>2QpO=@50SNe;>o#&n3u?%0}yFe*3ZkVNX^p^D9X=DO)e>(*ct)=BsmU? diff --git a/utils/database/__pycache__/database.cpython-310.pyc b/utils/database/__pycache__/database.cpython-310.pyc index fabbb5b541774c851531b2895a11ee4e894a153a..b437982045a8a2bb9e6c1c2f067546b5cb044e3d 100644 GIT binary patch delta 2657 zcma)8-)~b@9KYxG_O@%cvUOc)OWDdCu)<(JV6gE+R3^&A0ivRy)~@HYyItE$?!6*g ziUnr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xOT iBSazM{|g^IyH5mrB1X_`_XS%-vggazTDJJJr|*9QwOFkH 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 0e1edd71946103b5ce264d04c86833a137b8d3cf..a9a1ec8b2494f9f87756b5700d716385d1acfade 100644 GIT binary patch delta 52 zcmaFG^pS}>pO=@50SNe;>n3uqkxkRj$j?pH&&#Yx&C?Gk%FjwoE-BV`&dn3ui$;Rque*3ZkVNX^p^D9X=DO)e>(*cS!>D!LBC diff --git a/utils/middleware/__pycache__/__init__.cpython-310.pyc b/utils/middleware/__pycache__/__init__.cpython-310.pyc index 8746ad3f1a8028ef2da8b51f8d430d3bc4d08cce..d4b20c7fac300704c6cc0c12e1164be912af8e15 100644 GIT binary patch delta 52 zcmcb>c%6|upO=@50SNe;>n3tX%Eszv*r-wq~_@d6y;~7CYKaXYzzYcA2JSL diff --git a/utils/middleware/__pycache__/auth.cpython-310.pyc b/utils/middleware/__pycache__/auth.cpython-310.pyc index 97c9706f669c2e63cea6d63a54e6b933e7045df6..1270f95776ed863aecebe488e264bf558c273c84 100644 GIT binary patch delta 55 zcmeC>p2E$Y&&$ij00jKabsM?YGs{NmXXNLm>gQ!vq~_@d6y;~7CYKcJJLl)*q^75C JzQDYf5dfs<5t;x1 delta 48 zcmbQj-OJ6L&&$ij00b&K$~SVaXBG?64=qkDD%Q`-tVqq%4=BpdN=+^)-h7>TFCzdw Ch!0f& diff --git a/utils/models/__pycache__/__init__.cpython-310.pyc b/utils/models/__pycache__/__init__.cpython-310.pyc index 1044dea98b4939fb33e81465e48b7285d70fc59a..410b9a7013b81e530f74a55d22cde646d7a508e7 100644 GIT binary patch delta 52 zcmX@dc$JYmpO=@50SNe;>n3tX%SP&Fe*3ZkVNX^p^D9X=DO)e>(*c=1^CF>5C diff --git a/utils/models/__pycache__/models.cpython-310.pyc b/utils/models/__pycache__/models.cpython-310.pyc index 7e2264eb978d3da654753042558a3b9ba531d44d..9c4cd13aca45cd05c9c0341269b02e4e7480c427 100644 GIT binary patch delta 55 zcmbOtI$M-GpO=@50SNe;>o#)VVv-Hl&&bbB)z8bUNX^p^D9X=DO)e?cch1krNlj1P J{F`YeI{>ma5%2&2 delta 48 zcmbO&Iz^N_pO=@50SJzqF5k#~i%Bd%KeRZts8~NQvm!N5KcFZ-D>b>Kcrz38Om+ZW CIS*d| 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 0000000000000000000000000000000000000000..b770eaa1ea4fbd1f00162f9d9f701568002b63de GIT binary patch literal 211 zcmYjLK?=e!5KO9q2!(=Q@X|xw4~PifMerhCLTQ#Zl4eVig5La2ztpQIzu?KFCkJ+h z85m|MO;;>nGrX#G(04TdNP;|S7@k3FzSH@&X(?2c?U<*8fyeJwK9lSnYxfzTSOpRHR!x8J=vn{I_SO94yFwd YLK)?S*mloxxob)G8u#%H7l$+U11{k;?*IS* literal 0 HcmV?d00001 diff --git a/utils/tools/__pycache__/tools.cpython-310.pyc b/utils/tools/__pycache__/tools.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4305466f2a6c4d51725e3a15403225f6c0423d29 GIT binary patch literal 593 zcmY*WPiqu06n{x(y5rhHvDK3wz&UgW58lcm=%trMD5ba{GCO%YGn$!|YC(qslsV}=!^o94!OWymvKZ)Mk>jTNj`cpAr0N=ymU*U9kPHGPcBuJ)U z43bOn8OBVqQ%Iv9ow`!eHxEEHrC#nIm){(o4H!q%xQgxgYKhGj#b`FWIpVLe!|Q4;p$-{pAC+c zE^=juWNagTk`vM$->Y(4se0lCh51N#*!rk;X#CEslet5Gufj`NmsC9!KAyIDgX1Ky z-UdcoRI~Q|W-1d?N)t+(`vhT7`+y)}yX+AuzwO^9V*hs{HvW{G@M0T#%MXmL6_U_2 Rjg~Wo!|?GoI1|_ve*iHKgk=B# literal 0 HcmV?d00001 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