Generate cache files for compose generator

This commit is contained in:
AaronDewes 2022-05-16 09:09:23 +00:00
parent 611d4a166b
commit 3cec30340b
5 changed files with 56 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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))