From 3cec30340b5417605a4a1cc64a8d8366c3c39acb Mon Sep 17 00:00:00 2001 From: AaronDewes Date: Mon, 16 May 2022 09:09:23 +0000 Subject: [PATCH] Generate cache files for compose generator --- app/lib/citadelutils.py | 8 ++++---- app/lib/composegenerator/next/stage1.py | 24 ++++++++++++++++++++++ app/lib/composegenerator/shared/env.py | 19 ++++++++--------- app/lib/composegenerator/v3/generate.py | 27 +++++++++++++------------ app/lib/metadata.py | 4 ++++ 5 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 app/lib/composegenerator/next/stage1.py diff --git a/app/lib/citadelutils.py b/app/lib/citadelutils.py index 0d00813..fcc4478 100644 --- a/app/lib/citadelutils.py +++ b/app/lib/citadelutils.py @@ -75,7 +75,7 @@ def is_builtin_type(obj): def classToDict(theClass): obj: dict = {} for key, value in theClass.__dict__.items(): - if value is None or (isinstance(value, list) and len(value) == 0): + if type(value).__name__ == "NoneType" or (isinstance(value, list) and len(value) == 0): continue if isinstance(value, list): newList = [] @@ -83,7 +83,8 @@ def classToDict(theClass): if is_builtin_type(element): newList.append(element) else: - newList.append(classToDict(element)) + if type(element).__name__ != "NoneType": + newList.append(classToDict(element)) obj[key] = newList elif isinstance(value, dict): newDict = {} @@ -95,8 +96,7 @@ def classToDict(theClass): obj[key] = newDict elif is_builtin_type(value): obj[key] = value - else: - #print(value) + elif type(value).__name__ != "NoneType": obj[key] = classToDict(value) return obj diff --git a/app/lib/composegenerator/next/stage1.py b/app/lib/composegenerator/next/stage1.py new file mode 100644 index 0000000..ab0b825 --- /dev/null +++ b/app/lib/composegenerator/next/stage1.py @@ -0,0 +1,24 @@ +from lib.citadelutils import classToDict +from lib.composegenerator.shared.env import validateEnv + +from lib.composegenerator.v3.types import App, generateApp +from lib.composegenerator.v3.generate import convertContainerPermissions + +def createCleanConfigFromV3(app: dict, nodeRoot: str): + parsedApp: App = generateApp(app) + for container in range(len(parsedApp.containers)): + # TODO: Make this dynamic and not hardcoded + if parsedApp.containers[container].requires and "c-lightning" in parsedApp.containers[container].requires: + parsedApp.containers[container] = None + parsedApp = convertContainerPermissions(parsedApp) + parsedApp = validateEnv(parsedApp) + finalApp = classToDict(parsedApp) + try: + finalApp['permissions'] = finalApp['metadata']['dependencies'] + except: + finalApp['permissions'] = [] + finalApp['id'] = finalApp['metadata']['id'] + del finalApp['metadata'] + # Set version of the cache file format + finalApp['version'] = "1" + return finalApp diff --git a/app/lib/composegenerator/shared/env.py b/app/lib/composegenerator/shared/env.py index c95cf12..69b4318 100644 --- a/app/lib/composegenerator/shared/env.py +++ b/app/lib/composegenerator/shared/env.py @@ -40,13 +40,14 @@ def validateEnvStringOrListorDict(env: Union[str, Union[list, dict]], existingEn def validateEnv(app: App): # For every container of the app, check if all env vars in the strings in environment are defined in env for container in app.containers: - if container.environment_allow: - existingEnv = container.environment_allow - del container.environment_allow - else: - existingEnv = [] - if container.environment: - validateEnvStringOrListorDict(container.command, existingEnv, app.metadata.id, container.name) - validateEnvStringOrListorDict(container.entrypoint, existingEnv, app.metadata.id, container.name) - validateEnvStringOrListorDict(container.environment, existingEnv, app.metadata.id, container.name) + if container is not None: + if container.environment_allow: + existingEnv = container.environment_allow + del container.environment_allow + else: + existingEnv = [] + if container.environment: + validateEnvStringOrListorDict(container.command, existingEnv, app.metadata.id, container.name) + validateEnvStringOrListorDict(container.entrypoint, existingEnv, app.metadata.id, container.name) + validateEnvStringOrListorDict(container.environment, existingEnv, app.metadata.id, container.name) return app diff --git a/app/lib/composegenerator/v3/generate.py b/app/lib/composegenerator/v3/generate.py index 13cc7d2..014a769 100644 --- a/app/lib/composegenerator/v3/generate.py +++ b/app/lib/composegenerator/v3/generate.py @@ -16,20 +16,21 @@ from lib.composegenerator.shared.const import permissions def convertContainerPermissions(app: App) -> App: for container in app.containers: - for permission in app.metadata.dependencies: - if isinstance(permission, str): - if permission in permissions(): - container.environment_allow.extend(permissions()[permission]['environment_allow']) - container.volumes.extend(permissions()[permission]['volumes']) - else: - print("Warning: app {} defines unknown permission {}".format(app.metadata.name, permission)) - else: - for subPermission in permission: - if subPermission in permissions(): - container.environment_allow.extend(permissions()[subPermission]['environment_allow']) - container.volumes.extend(permissions()[subPermission]['volumes']) + if container is not None: + for permission in app.metadata.dependencies: + if isinstance(permission, str): + if permission in permissions(): + container.environment_allow.extend(permissions()[permission]['environment_allow']) + container.volumes.extend(permissions()[permission]['volumes']) else: - print("Warning: app {} defines unknown permission {}".format(app.metadata.name, subPermission)) + print("Warning: app {} defines unknown permission {}".format(app.metadata.name, permission)) + else: + for subPermission in permission: + if subPermission in permissions(): + container.environment_allow.extend(permissions()[subPermission]['environment_allow']) + container.volumes.extend(permissions()[subPermission]['volumes']) + else: + print("Warning: app {} defines unknown permission {}".format(app.metadata.name, subPermission)) return app def convertDataDirToVolumeGen3(app: App) -> AppStage2: diff --git a/app/lib/metadata.py b/app/lib/metadata.py index b5c393d..b36e4e1 100644 --- a/app/lib/metadata.py +++ b/app/lib/metadata.py @@ -5,6 +5,7 @@ import os import yaml +from lib.composegenerator.next.stage1 import createCleanConfigFromV3 from lib.composegenerator.v2.networking import getMainContainer from lib.composegenerator.v1.networking import getFreePort from lib.entropy import deriveEntropy @@ -40,6 +41,7 @@ def getAppRegistry(apps, app_path): app_metadata = [] for app in apps: app_yml_path = os.path.join(app_path, app, 'app.yml') + app_cache_path = os.path.join(app_path, app, 'app.cache.json') if os.path.isfile(app_yml_path): try: with open(app_yml_path, 'r') as f: @@ -57,6 +59,8 @@ def getAppRegistry(apps, app_path): getPortsOldApp(app_yml, app) else: getPortsV3App(app_yml, app) + with open(app_cache_path, 'w') as f: + json.dump(createCleanConfigFromV3(app_yml, os.path.dirname(app_path)), f) except Exception as e: print(e) print("App {} is invalid!".format(app))