diff --git a/messages/v1/messages/applicationservices.create.yml b/messages/v1/messages/applicationservice.create.yml similarity index 100% rename from messages/v1/messages/applicationservices.create.yml rename to messages/v1/messages/applicationservice.create.yml diff --git a/messages/v1/messages/applicationservices.dataset.updated.yml b/messages/v1/messages/applicationservice.dataset.updated.yml similarity index 83% rename from messages/v1/messages/applicationservices.dataset.updated.yml rename to messages/v1/messages/applicationservice.dataset.updated.yml index 62f5f29..dc0cfba 100644 --- a/messages/v1/messages/applicationservices.dataset.updated.yml +++ b/messages/v1/messages/applicationservice.dataset.updated.yml @@ -1,4 +1,4 @@ -uri: applicationservices.dataset.updated +uri: applicationservice.dataset.updated description: | Initialise la table pour les services applicatifs. @@ -7,8 +7,6 @@ pattern: rpc public: true -domain: applicationservices-domain - parameters: release_path: type: String diff --git a/messages/v1/messages/config.configuration.server.deploy.yml b/messages/v1/messages/config.configuration.server.deploy.yml index 81e7b78..f2c91bb 100644 --- a/messages/v1/messages/config.configuration.server.deploy.yml +++ b/messages/v1/messages/config.configuration.server.deploy.yml @@ -4,14 +4,10 @@ uri: config.configuration.server.deploy description: | Déployer la configuration d'un serveur. -sampleuse: ~ - pattern: event public: false -domain: config-domain - parameters: server_id: type: Number diff --git a/messages/v1/messages/config.configuration.server.get.yml b/messages/v1/messages/config.configuration.server.get.yml index 8305ed1..9a6332c 100644 --- a/messages/v1/messages/config.configuration.server.get.yml +++ b/messages/v1/messages/config.configuration.server.get.yml @@ -4,14 +4,10 @@ uri: config.configuration.server.get description: | Récupère le contenu de la configuration. -sampleuse: ~ - pattern: rpc public: false -domain: config-domain - parameters: server_id: type: Number diff --git a/messages/v1/messages/config.configuration.server.updated.yml b/messages/v1/messages/config.configuration.server.updated.yml index 65f38f3..030996d 100644 --- a/messages/v1/messages/config.configuration.server.updated.yml +++ b/messages/v1/messages/config.configuration.server.updated.yml @@ -4,14 +4,10 @@ uri: config.configuration.server.updated description: | Une configuration de serveur a été mise à jour. -sampleuse: ~ - pattern: event public: false -domain: config-domain - parameters: server_id: type: Number diff --git a/messages/v1/messages/server.created.yml b/messages/v1/messages/server.created.yml index e5477bf..9c234d1 100644 --- a/messages/v1/messages/server.created.yml +++ b/messages/v1/messages/server.created.yml @@ -7,8 +7,6 @@ pattern: event public: false -domain: server-domain - parameters: type: Server description: Description du serveur. diff --git a/messages/v1/messages/server.deleted.yml b/messages/v1/messages/server.deleted.yml index d0c9ac5..e348b0f 100644 --- a/messages/v1/messages/server.deleted.yml +++ b/messages/v1/messages/server.deleted.yml @@ -1,8 +1,6 @@ --- uri: server.deleted -sampleuse: ~ - description: | Un serveur a été supprimé. @@ -10,8 +8,6 @@ pattern: event public: false -domain: server-domain - parameters: server_id: type: Number diff --git a/messages/v1/messages/server.list.yml b/messages/v1/messages/server.list.yml index 9c5fa82..730d37d 100644 --- a/messages/v1/messages/server.list.yml +++ b/messages/v1/messages/server.list.yml @@ -4,15 +4,10 @@ uri: server.list description: | Liste les serveurs disponibles. -sampleuse: | - zephir-client server.list - public: true pattern: rpc -domain: server-domain - response: type: '[]Server' description: Retourne la liste des serveurs. diff --git a/messages/v1/messages/servermodel.created.yml b/messages/v1/messages/servermodel.created.yml index 90d55c4..b2a82e3 100644 --- a/messages/v1/messages/servermodel.created.yml +++ b/messages/v1/messages/servermodel.created.yml @@ -7,8 +7,6 @@ pattern: event public: false -domain: servermodel-domain - parameters: type: Servermodel description: Informations sur les modèles de serveur créés. diff --git a/messages/v1/messages/servermodel.dataset.updated.yml b/messages/v1/messages/servermodel.dataset.updated.yml index a2551cf..d0efe3e 100644 --- a/messages/v1/messages/servermodel.dataset.updated.yml +++ b/messages/v1/messages/servermodel.dataset.updated.yml @@ -7,8 +7,6 @@ pattern: rpc public: true -domain: servermodel-domain - parameters: release_path: type: String diff --git a/messages/v1/messages/servermodel.describe.yml b/messages/v1/messages/servermodel.describe.yml index 6e82436..3b0e6ef 100644 --- a/messages/v1/messages/servermodel.describe.yml +++ b/messages/v1/messages/servermodel.describe.yml @@ -4,15 +4,10 @@ uri: servermodel.describe description: | Retourne les attributs détaillés d'un modèle de serveur. -sampleuse: | - zephir-client servermodel.describe -s 1 - pattern: rpc public: true -domain: servermodel-domain - parameters: servermodelid: type: Number diff --git a/messages/v1/messages/servermodel.list.yml b/messages/v1/messages/servermodel.list.yml index 6e0964c..05be732 100644 --- a/messages/v1/messages/servermodel.list.yml +++ b/messages/v1/messages/servermodel.list.yml @@ -4,15 +4,10 @@ uri: servermodel.list description: | Retourne la liste des modèles de serveur disponibles. -sampleuse: | - zephir-client servermodel.list - pattern: rpc public: true -domain: servermodel-domain - parameters: sourceid: type: Number diff --git a/messages/v1/messages/servermodel.updated.yml b/messages/v1/messages/servermodel.updated.yml index 531c18e..82a1078 100644 --- a/messages/v1/messages/servermodel.updated.yml +++ b/messages/v1/messages/servermodel.updated.yml @@ -7,8 +7,6 @@ pattern: event public: false -domain: servermodel-domain - parameters: type: 'Servermodel' description: Informations sur les modèles de serveur modifiés. diff --git a/messages/v1/messages/session.server.configure.yml b/messages/v1/messages/session.server.configure.yml index d80141d..db06ab4 100644 --- a/messages/v1/messages/session.server.configure.yml +++ b/messages/v1/messages/session.server.configure.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: type: String diff --git a/messages/v1/messages/session.server.filter.yml b/messages/v1/messages/session.server.filter.yml index 770ac2d..36b0aa9 100644 --- a/messages/v1/messages/session.server.filter.yml +++ b/messages/v1/messages/session.server.filter.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: type: String diff --git a/messages/v1/messages/session.server.get.yml b/messages/v1/messages/session.server.get.yml index 33dc568..058520e 100644 --- a/messages/v1/messages/session.server.get.yml +++ b/messages/v1/messages/session.server.get.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: type: String diff --git a/messages/v1/messages/session.server.list.yml b/messages/v1/messages/session.server.list.yml index 80dce42..6d23343 100644 --- a/messages/v1/messages/session.server.list.yml +++ b/messages/v1/messages/session.server.list.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - response: type: '[]Session' description: | diff --git a/messages/v1/messages/session.server.start.yml b/messages/v1/messages/session.server.start.yml index c88ed98..dd5867f 100644 --- a/messages/v1/messages/session.server.start.yml +++ b/messages/v1/messages/session.server.start.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: id: type: Number diff --git a/messages/v1/messages/session.server.stop.yml b/messages/v1/messages/session.server.stop.yml index 72876a3..69a65a1 100644 --- a/messages/v1/messages/session.server.stop.yml +++ b/messages/v1/messages/session.server.stop.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: ref: Config.SessionId diff --git a/messages/v1/messages/session.server.validate.yml b/messages/v1/messages/session.server.validate.yml index 383312c..98552e5 100644 --- a/messages/v1/messages/session.server.validate.yml +++ b/messages/v1/messages/session.server.validate.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: ref: Config.SessionId diff --git a/messages/v1/messages/session.servermodel.configure.yml b/messages/v1/messages/session.servermodel.configure.yml index 4cb0c9b..8cbc0e2 100644 --- a/messages/v1/messages/session.servermodel.configure.yml +++ b/messages/v1/messages/session.servermodel.configure.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: type: String diff --git a/messages/v1/messages/session.servermodel.filter.yml b/messages/v1/messages/session.servermodel.filter.yml index c14b075..84723a5 100644 --- a/messages/v1/messages/session.servermodel.filter.yml +++ b/messages/v1/messages/session.servermodel.filter.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: type: String diff --git a/messages/v1/messages/session.servermodel.get.yml b/messages/v1/messages/session.servermodel.get.yml index aac98a8..4e14e17 100644 --- a/messages/v1/messages/session.servermodel.get.yml +++ b/messages/v1/messages/session.servermodel.get.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: type: String diff --git a/messages/v1/messages/session.servermodel.list.yml b/messages/v1/messages/session.servermodel.list.yml index 574585d..dfec232 100644 --- a/messages/v1/messages/session.servermodel.list.yml +++ b/messages/v1/messages/session.servermodel.list.yml @@ -7,8 +7,6 @@ pattern: rpc public: true -domain: session-domain - response: type: '[]Session' description: | diff --git a/messages/v1/messages/session.servermodel.start.yml b/messages/v1/messages/session.servermodel.start.yml index 960ae01..b18438d 100644 --- a/messages/v1/messages/session.servermodel.start.yml +++ b/messages/v1/messages/session.servermodel.start.yml @@ -7,8 +7,6 @@ pattern: rpc public: true -domain: session-domain - parameters: id: type: Number diff --git a/messages/v1/messages/session.servermodel.stop.yml b/messages/v1/messages/session.servermodel.stop.yml index d41b084..6402cb7 100644 --- a/messages/v1/messages/session.servermodel.stop.yml +++ b/messages/v1/messages/session.servermodel.stop.yml @@ -7,8 +7,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: ref: Config.SessionId diff --git a/messages/v1/messages/session.servermodel.validate.yml b/messages/v1/messages/session.servermodel.validate.yml index 1fe0078..1518af8 100644 --- a/messages/v1/messages/session.servermodel.validate.yml +++ b/messages/v1/messages/session.servermodel.validate.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: session-domain - parameters: session_id: ref: Config.SessionId diff --git a/messages/v1/messages/source.dataset.updated.yml b/messages/v1/messages/source.dataset.updated.yml index 3a40bac..0a6f393 100644 --- a/messages/v1/messages/source.dataset.updated.yml +++ b/messages/v1/messages/source.dataset.updated.yml @@ -7,8 +7,6 @@ pattern: rpc public: true -domain: source-domain - parameters: source_name: type: String diff --git a/messages/v1/messages/source.release.list.yml b/messages/v1/messages/source.release.list.yml index b2f9ee6..8bc920e 100644 --- a/messages/v1/messages/source.release.list.yml +++ b/messages/v1/messages/source.release.list.yml @@ -8,8 +8,6 @@ pattern: rpc public: true -domain: source-domain - response: type: '[]Release' description: Liste des versions disponibles. diff --git a/messages/v1/messages/template.generate.yml b/messages/v1/messages/template.generate.yml index 0b9b2a2..0d9b21c 100644 --- a/messages/v1/messages/template.generate.yml +++ b/messages/v1/messages/template.generate.yml @@ -4,14 +4,10 @@ uri: template.generate description: | Génère et récupère les templates générés. -sampleuse: ~ - pattern: rpc public: true -domain: template-domain - parameters: server_id: type: Number diff --git a/messages/v1/types/applicationservice.yml b/messages/v1/types/applicationservice.yml index 0789201..9a50344 100644 --- a/messages/v1/types/applicationservice.yml +++ b/messages/v1/types/applicationservice.yml @@ -19,7 +19,7 @@ properties: applicationservice_dependencies: type: array items: - type: integer + type: number description: Liste des services applicatifs déclarés en dépendance de ce service applicatif. required: diff --git a/messages/v1/types/servermodel.yml b/messages/v1/types/servermodel.yml index c963f44..83512e7 100644 --- a/messages/v1/types/servermodel.yml +++ b/messages/v1/types/servermodel.yml @@ -3,55 +3,28 @@ title: Servermodel type: object description: Description d'un modèle de serveur. properties: - servermodelid: + servermodel_id: type: number description: ID du modèle de serveur. ref: Servermodel.ServermodelId - servermodelname: + servermodel_name: type: string description: Nom du modèle de serveur. - servermodeldescription: + servermodel_description: type: string description: Description du modèle de serveur. - servermodelparentsid: + servermodel_parents_id: type: array items: type: number ref: Servermodel.ServermodelId description: ID du modèle de serveur parent. - subreleaseid: + release_id: type: number ref: Servermodel.SubreleaseId description: Version du modèle de serveur. - subreleasename: - type: string - ref: SubRelease.SubReleaseName - description: Nom de la sous-version. - sourceid: - type: number - ref: ServermodelId.SourceId - description: ID de la sous-version. - services: - type: array - items: - type: object - description: Liste des services applicatifs déclarés pour ce modèle de serveur. - schema: - type: string - description: Contenu du schema. - probes: - type: string - description: Informations sur les sondes. - creolefuncs: - type: string - description: Fonctions Creole. - conffiles: - type: string - description: Fichiers creole au format tar encodé base64 required: - - servermodelid - - servermodelname - - servermodeldescription - - servermodelsubreleaseid - - sourceid - - subreleasename + - servermodel_id + - servermodel_name + - servermodel_description + - release_id diff --git a/script/database_manager.py b/script/database_manager.py index b2d9eb1..c980d65 100644 --- a/script/database_manager.py +++ b/script/database_manager.py @@ -19,16 +19,16 @@ CREATE TABLE Release ( FOREIGN KEY (ReleaseSourceId) REFERENCES Source(SourceId) ); --- Création de la table ServerModel -CREATE TABLE ServerModel ( - ServerModelId SERIAL PRIMARY KEY, - ServerModelName VARCHAR(255) NOT NULL, - ServerModelDescription VARCHAR(255) NOT NULL, - ServerModelParentsId INTEGER [] DEFAULT '{}', - ServerModelReleaseId INTEGER NOT NULL, - ServerModelApplicationServiceId INTEGER NOT NULL, - ServerModelUsers hstore, - UNIQUE (ServerModelName, ServerModelReleaseId) +-- Création de la table Servermodel +CREATE TABLE Servermodel ( + ServermodelId SERIAL PRIMARY KEY, + ServermodelName VARCHAR(255) NOT NULL, + ServermodelDescription VARCHAR(255) NOT NULL, + ServermodelParentsId INTEGER [] DEFAULT '{}', + ServermodelReleaseId INTEGER NOT NULL, + ServermodelApplicationServiceId INTEGER NOT NULL, + ServermodelUsers hstore, + UNIQUE (ServermodelName, ServermodelReleaseId) ); -- Création de la table ApplicationService @@ -80,5 +80,5 @@ async def main(): async with pool.acquire() as connection: async with connection.transaction(): returns = await connection.execute(VERSION_INIT) - + asyncio.run(main()) diff --git a/src/risotto/config.py b/src/risotto/config.py index de0e8f6..44dfdc2 100644 --- a/src/risotto/config.py +++ b/src/risotto/config.py @@ -8,7 +8,7 @@ CONFIGURATION_DIR = 'configurations' TEMPLATE_DIR = 'templates' TMP_DIR = 'tmp' ROUGAIL_DTD_PATH = '../rougail/data/creole.dtd' -POSTGRESQL_ADDRESS = 'localhost' +POSTGRESQL_ADDRESS = '192.168.56.106' POSTGRESQL_PORT = 5432 def get_config(): diff --git a/src/risotto/dispatcher.py b/src/risotto/dispatcher.py index ec5f7fa..5590bfb 100644 --- a/src/risotto/dispatcher.py +++ b/src/risotto/dispatcher.py @@ -97,7 +97,10 @@ class CallDispatcher: risotto_context.function = function_obj['function'] if function_obj['risotto_context']: kw['risotto_context'] = risotto_context - if function_obj['database']: + # do not start a new database connection + if function_obj['database'] and hasattr(old_risotto_context, 'connection'): + risotto_context.connection = old_risotto_context.connection + if function_obj['database'] and not hasattr(risotto_context, 'connection'): db_conf = get_config().get('database') pool = await asyncpg.create_pool(database=db_conf.get('dbname'), user=db_conf.get('user')) async with pool.acquire() as connection: diff --git a/src/risotto/message/message.py b/src/risotto/message/message.py index de5b676..784d78e 100644 --- a/src/risotto/message/message.py +++ b/src/risotto/message/message.py @@ -42,8 +42,6 @@ class MessageDefinition: __slots__ = ('version', 'uri', 'description', - 'sampleuse', - 'domain', 'parameters', 'public', 'errors', @@ -59,7 +57,6 @@ class MessageDefinition: self.errors = [] self.related = [] self.response = None - self.sampleuse = None # loads yaml information into object for key, value in raw_def.items(): @@ -428,7 +425,7 @@ def _get_option(name, 'expected': ParamValue(optiondescription), 'reverse_condition': ParamValue(True)}), calc_value_property_help)) - + description = arg.description.strip().rstrip() kwargs = {'name': name, 'doc': _get_description(description, name), @@ -502,7 +499,7 @@ def _parse_responses(message_def, # FIXME 'File': StrOption}.get(type_) if not option: - raise Exception(f'unknown param type {obj.type}') + raise Exception(f'unknown param type {obj.type} in responses of message {message_def.uri}') if hasattr(obj, 'default'): kwargs['default'] = obj.default else: diff --git a/src/risotto/services/applicationservice/__init__.py b/src/risotto/services/applicationservice/__init__.py new file mode 100644 index 0000000..735209d --- /dev/null +++ b/src/risotto/services/applicationservice/__init__.py @@ -0,0 +1 @@ +from .applicationservice import Risotto diff --git a/src/risotto/services/applicationservice/applicationservice.py b/src/risotto/services/applicationservice/applicationservice.py new file mode 100644 index 0000000..9b91f01 --- /dev/null +++ b/src/risotto/services/applicationservice/applicationservice.py @@ -0,0 +1,70 @@ +from os import listdir +from os.path import join +from traceback import print_exc +from yaml import load, SafeLoader +from typing import Dict + +from ...controller import Controller +from ...register import register +from ...config import get_config +from ...error import ExecutionError +from ...context import Context +from ...utils import _ + +class Risotto(Controller): + async def _applicationservice_create(self, + risotto_context: Context, + applicationservice_name: str, + applicationservice_description: str, + release_id: int) -> Dict: + applicationservice_update_query = """INSERT INTO ApplicationService(ApplicationServiceName, ApplicationServiceDescription, ApplicationServiceReleaseId) VALUES ($1,$2,$3) + RETURNING ApplicationServiceId + """ + applicationservice_id = await risotto_context.connection.fetchval(applicationservice_update_query, + applicationservice_name, + applicationservice_description, + release_id) + return {'applicationservice_name': applicationservice_name, + 'applicationservice_description': applicationservice_description, + 'applicationservice_id': applicationservice_id} + + @register('v1.applicationservice.create', None, database=True) + async def applicationservice_create(self, + risotto_context: Context, + applicationservice_name: str, + applicationservice_description: str, + release_id: int) -> Dict: + return await self._applicationservice_create(risotto_context, + applicationservice_name, + applicationservice_description, + release_id) + + @register('v1.applicationservice.dataset.updated', None, database=True) + async def applicationservice_update(self, + risotto_context, + release_path: str, + release_id: int) -> Dict: + applicationservice_path = join(release_path, 'applicationservice') + for service in listdir(applicationservice_path): + try: + applicationservice_description_path = join(applicationservice_path, + service, + 'applicationservice.yml') + with open(applicationservice_description_path, 'r') as applicationservice_yml: + applicationservice_description = load(applicationservice_yml, + Loader=SafeLoader) + except Exception as err: + if get_config().get('global').get('debug'): + print_exc() + raise ExecutionError(_(f'Error while reading {applicationservice_description_path}: {err}')) + try: + await self._applicationservice_create(risotto_context, + applicationservice_description['name'], + applicationservice_description['description'], + release_id) + except Exception as err: + if get_config().get('global').get('debug'): + print_exc() + raise ExecutionError(_(f"Error while injecting application service {applicationservice_description['name']} in database: {err}")) + return {'retcode': 0, + 'returns': _('Application Services successfully loaded')} diff --git a/src/risotto/services/applicationservices/__init__.py b/src/risotto/services/applicationservices/__init__.py deleted file mode 100644 index 7f6f603..0000000 --- a/src/risotto/services/applicationservices/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .application_services import Risotto diff --git a/src/risotto/services/applicationservices/applicationservices.py b/src/risotto/services/applicationservices/applicationservices.py deleted file mode 100644 index b63bc80..0000000 --- a/src/risotto/services/applicationservices/applicationservices.py +++ /dev/null @@ -1,31 +0,0 @@ -from ...controller import Controller -from ...register import register - -class Risotto(Controller): - @register('v1.applicationservice.create', None, database=True) - async def applicationservice_create(self, risotto_context, applicationservice_name, applicationservice_description, release_id): - applicationservice_update_query = """INSERT INTO ApplicationService(ApplicationServiceName, ApplicationServiceDescription, ApplicationServiceReleaseId) VALUES ($1,$2,$3) - RETURNING ApplicationServiceId - """ - applicationservice_id = await risotto_context.connection.fetchval(applicationservice_update_query, applicationservice_description['name'], applicationservice_description['description'], release_id) - return {'applicationservice_name': applicationservice_name, 'applicationservice_description': applicationservice_description, 'applicationservice_id': applicationservice_id} - - @register('v1.applicationservice.dataset.updated', None, database=True) - async def applicationservice_update(self, risotto_context, release_path, release_id): - applicationservice_path = os.path.join(release_path, 'applicationservice') - for service in os.listdir(applicationservice_path): - try: - applicationservice_description_path = os.path.join(applicationservice_path, service, 'applicationservice.yml') - with open(applicationservice_description_path, 'r') as applicationservice_yml: - applicationservice_description = yaml.load(applicationservice_yml, Loader=yaml.SafeLoader) - except Exception as err: - if get_config().get('global').get('debug'): - print_exc() - raise ExecutionError(_(f'Error while reading {applicationservice_description_path}: {err}')) - try: - await self.applicationservice_create(risotto_context, applicationservice_description['name'], applicationservice_description['description'], release_id) - except Exception as err: - if get_config().get('global').get('debug'): - print_exc() - raise ExecutionError(_(f"Error while injecting application service {applicationservice_description['name']} in database: {err}")) - return {'retcode': 0, 'returns': _('Application Services successfully loaded')} diff --git a/src/risotto/services/config/config.py b/src/risotto/services/config/config.py index 673e9d4..85d8d2e 100644 --- a/src/risotto/services/config/config.py +++ b/src/risotto/services/config/config.py @@ -18,14 +18,14 @@ from ...logger import log class Risotto(Controller): - servermodel = {} - server = {} def __init__(self) -> None: for dirname in [ROOT_CACHE_DIR, DATABASE_DIR]: if not isdir(dirname): raise RegistrationError(_(f'unable to find the cache dir "{dirname}"')) self.save_storage = Storage(engine='sqlite3', dir_database=DATABASE_DIR) + self.servermodel = {} + self.server = {} super().__init__() async def on_join(self, @@ -49,37 +49,37 @@ class Risotto(Controller): for servermodel in servermodels: try: await self.load_servermodel(risotto_context, - servermodel['servermodelid'], - servermodel['servermodelname']) + servermodel['servermodel_id'], + servermodel['servermodel_name']) except CallError as err: pass # do link to this servermodel for servermodel in servermodels: - if 'servermodelparentsid' in servermodel: - for servermodelparentid in servermodel['servermodelparentsid']: + if 'servermodel_parents_id' in servermodel: + for servermodelparentid in servermodel['servermodel_parents_id']: self.servermodel_legacy(risotto_context, - servermodel['servermodelname'], - servermodel['servermodelid'], + servermodel['servermodel_name'], + servermodel['servermodel_id'], servermodelparentid) def get_funcs_filename(self, - servermodelid: int): - return join(ROOT_CACHE_DIR, str(servermodelid)+".creolefuncs") + servermodel_id: int): + return join(ROOT_CACHE_DIR, str(servermodel_id)+".creolefuncs") async def load_servermodel(self, risotto_context: Context, - servermodelid: int, - servermodelname: str) -> None: + servermodel_id: int, + servermodel_name: str) -> None: """ Loads a servermodel """ - cache_file = join(ROOT_CACHE_DIR, str(servermodelid)+".xml") - funcs_file = self.get_funcs_filename(servermodelid) + cache_file = join(ROOT_CACHE_DIR, str(servermodel_id)+".xml") + funcs_file = self.get_funcs_filename(servermodel_id) log.info_msg(risotto_context, None, - f'Load servermodel {servermodelname} ({servermodelid})') + f'Load servermodel {servermodel_name} ({servermodel_id})') # use file in cache if found, otherwise retrieve it in servermodel context if isfile(cache_file): @@ -87,7 +87,7 @@ class Risotto(Controller): else: servermodel = await self.call('v1.servermodel.describe', risotto_context, - servermodelid=servermodelid, + servermodel_id=servermodel_id, inheritance=False, resolvdepends=False, schema=True, @@ -104,20 +104,20 @@ class Risotto(Controller): # loads tiramisu config and store it xmlroot = parse(fileio).getroot() - self.servermodel[servermodelid] = self.build_metaconfig(servermodelid, - servermodelname, + self.servermodel[servermodel_id] = self.build_metaconfig(servermodel_id, + servermodel_name, xmlroot, funcs_file) def build_metaconfig(self, - servermodelid: int, - servermodelname: str, + servermodel_id: int, + servermodel_name: str, xmlroot: str, funcs_file: str) -> MetaConfig: """ Build metaconfig for a servermodel """ # build tiramisu's session ID - session_id = f'v_{servermodelid}' + session_id = f'v_{servermodel_id}' optiondescription = rougail_load(xmlroot, ROUGAIL_DTD_PATH, funcs_file) @@ -149,9 +149,9 @@ class Risotto(Controller): metaconfig.permissive.add('expert') # set informtion and owner - metaconfig.owner.set('v_{}'.format(servermodelname)) - metaconfig.information.set('servermodel_id', servermodelid) - metaconfig.information.set('servermodel_name', servermodelname) + metaconfig.owner.set('v_{}'.format(servermodel_name)) + metaconfig.information.set('servermodel_id', servermodel_id) + metaconfig.information.set('servermodel_name', servermodel_name) # return configuration return metaconfig @@ -221,7 +221,7 @@ class Risotto(Controller): risotto_context: Context, server_id: int, servername: str, - servermodelid: int) -> None: + servermodel_id: int) -> None: """ Loads a server """ if server_id in self.server: @@ -229,18 +229,18 @@ class Risotto(Controller): log.info_msg(risotto_context, None, f'Load server {servername} ({server_id})') - if not servermodelid in self.servermodel: - msg = f'unable to find servermodel with id {servermodelid}' + if not servermodel_id in self.servermodel: + msg = f'unable to find servermodel with id {servermodel_id}' log.error_msg(risotto_context, None, msg) raise CallError(msg) - + # check if server was already created session_id = f's_{server_id}' # get the servermodel's metaconfig - metaconfig = self.servermodel[servermodelid] + metaconfig = self.servermodel[servermodel_id] # create server configuration and server 'to deploy' configuration and store it self.server[server_id] = {'server': self.build_config(session_id, @@ -251,7 +251,7 @@ class Risotto(Controller): server_id, servername, metaconfig), - 'funcs_file': self.get_funcs_filename(servermodelid)} + 'funcs_file': self.get_funcs_filename(servermodel_id)} def build_config(self, session_id: str, @@ -280,7 +280,7 @@ class Risotto(Controller): self.load_server(risotto_context, server_id, servername, - servermodelid) + servermodel_id) @register('v1.server.deleted') async def server_deleted(self, @@ -298,35 +298,35 @@ class Risotto(Controller): @register('v1.servermodel.created') async def servermodel_created(self, risotto_context: Context, - servermodelid: int, - servermodelname: str, - servermodelparentsid: List[int]) -> None: + servermodel_id: int, + servermodel_name: str, + servermodel_parents_id: List[int]) -> None: """ when servermodels are created, load it and do link """ await self.load_and_link_servermodel(risotto_context, - servermodelid, - servermodelname, - servermodelparentsid) + servermodel_id, + servermodel_name, + servermodel_parents_id) async def load_and_link_servermodel(self, risotto_context: Context, - servermodelid: int, - servermodelname: str, - servermodelparentsid: List[int]) -> None: + servermodel_id: int, + servermodel_name: str, + servermodel_parents_id: List[int]) -> None: await self.load_servermodel(risotto_context, - servermodelid, - servermodelname) - if servermodelparentsid is not None: - for servermodelparentid in servermodelparentsid: + servermodel_id, + servermodel_name) + if servermodel_parents_id is not None: + for servermodelparentid in servermodel_parents_id: self.servermodel_legacy(risotto_context, - servermodelname, - servermodelid, + servermodel_name, + servermodel_id, servermodelparentid) def servermodel_delete(self, - servermodelid: int) -> List[MetaConfig]: - metaconfig = self.servermodel.pop(servermodelid) + servermodel_id: int) -> List[MetaConfig]: + metaconfig = self.servermodel.pop(servermodel_id) mixconfig = next(metaconfig.config.list()) children = [] for child in mixconfig.config.list(): @@ -345,42 +345,42 @@ class Risotto(Controller): @register('v1.servermodel.updated') async def servermodel_updated(self, risotto_context: Context, - servermodelid: int, - servermodelname: str, - servermodelparentsid: List[int]) -> None: + servermodel_id: int, + servermodel_name: str, + servermodel_parents_id: List[int]) -> None: log.info_msg(risotto_context, None, - f'Reload servermodel {servermodelname} ({servermodelid})') + f'Reload servermodel {servermodel_name} ({servermodel_id})') # unlink cache to force download new aggregated file - cache_file = join(ROOT_CACHE_DIR, str(servermodelid)+".xml") + cache_file = join(ROOT_CACHE_DIR, str(servermodel_id)+".xml") if isfile(cache_file): unlink(cache_file) # store all informations - if servermodelid in self.servermodel: - old_values = self.servermodel[servermodelid].value.exportation() - old_permissives = self.servermodel[servermodelid].permissive.exportation() - old_properties = self.servermodel[servermodelid].property.exportation() - children = self.servermodel_delete(servermodelid) + if servermodel_id in self.servermodel: + old_values = self.servermodel[servermodel_id].value.exportation() + old_permissives = self.servermodel[servermodel_id].permissive.exportation() + old_properties = self.servermodel[servermodel_id].property.exportation() + children = self.servermodel_delete(servermodel_id) else: old_values = None # create new one await self.load_and_link_servermodel(risotto_context, - servermodelid, - servermodelname, - servermodelparentsid) + servermodel_id, + servermodel_name, + servermodel_parents_id) # migrates informations if old_values is not None: - self.servermodel[servermodelid].value.importation(old_values) - self.servermodel[servermodelid].permissive.importation(old_permissives) - self.servermodel[servermodelid].property.importation(old_properties) + self.servermodel[servermodel_id].value.importation(old_values) + self.servermodel[servermodel_id].permissive.importation(old_permissives) + self.servermodel[servermodel_id].property.importation(old_properties) for child in children: self.servermodel_legacy(risotto_context, child.information.get('servermodel_name'), child.information.get('servermodel_id'), - servermodelid) + servermodel_id) @register('v1.config.configuration.server.get', None) async def get_configuration(self, diff --git a/src/risotto/services/servermodel/servermodel.py b/src/risotto/services/servermodel/servermodel.py index 234f016..64c93ce 100644 --- a/src/risotto/services/servermodel/servermodel.py +++ b/src/risotto/services/servermodel/servermodel.py @@ -1,40 +1,70 @@ +from os import listdir +from os.path import join +from yaml import load, SafeLoader +from traceback import print_exc from ...controller import Controller from ...register import register from ...utils import _ -import os -import yaml -from traceback import print_exc +from ...context import Context from ...config import get_config from ...error import ExecutionError class Risotto(Controller): @register('v1.servermodel.dataset.updated', None, database=True) - async def servermodel_update(self, risotto_context, release_path, release_id, applicationservice_įd): - applicationservice_update = """INSERT INTO ApplicationService(ApplicationServiceName, ApplicationServiceDescription, ApplicationServiceReleaseId) VALUES ($1,$2,$3) - RETURNING ApplicationServiceId + async def servermodel_update(self, + risotto_context: Context, + release_path: str, + release_id: int): + servermodel_update = """INSERT INTO Servermodel(ServermodelName, ServermodelDescription, ServermodelParentsId, ServermodelReleaseId, ServermodelApplicationServiceId) VALUES ($1,$2,$3,$4,$5) + RETURNING ServermodelId """ - servermodel_path = os.path.join(release_path, 'servermodel') - for servermodel in os.listdir(servermodel_path): + servermodel_path = join(release_path, 'servermodel') + for servermodel in listdir(servermodel_path): + if not servermodel.endswith('.yml'): + continue + servermodel_description_path = join(servermodel_path, servermodel) try: - with open(os.path.join(servermodel_path, servermodel), 'r') as applicationservice_yml: - applicationservice_description = yaml.load(applicationservice_yml, Loader=yaml.SafeLoader) + with open(servermodel_description_path, 'r') as servermodel_yml: + servermodel_description = load(servermodel_yml, + Loader=SafeLoader) except Exception as err: if get_config().get('global').get('debug'): print_exc() - raise ExecutionError(_(f'Error while reading {applicationservice_description_path}: {err}')) + raise ExecutionError(_(f'Error while reading {servermodel_description_path}: {err}')) + sm_name = servermodel_description['name'] + sm_description = servermodel_description['description'] + sm_parentsid = servermodel_description['parent'] + if sm_parentsid is None: + sm_parentsid = [] + else: + sm_parentsid = [sm_parentsid] + as_name = f"local_{sm_name}" + as_description = f'local application service for {sm_name}' + applicationservice = await self.call('v1.applicationservice.create', + risotto_context, + applicationservice_name=as_name, + applicationservice_description=as_description, + release_id=release_id) try: - await risotto_context.connection.fetch(applicationservice_update, applicationservice_description['name'], applicationservice_description['description'], release_id) + await risotto_context.connection.fetch(servermodel_update, + sm_name, + sm_description, + sm_parentsid, + release_id, + applicationservice['applicationservice_id']) except Exception as err: if get_config().get('global').get('debug'): print_exc() - raise ExecutionError(_(f"Error while injecting application service {applicationservice_description['name']} in database: {err}")) - return {'retcode': 0, 'returns': _('Application Services successfully loaded')} + raise ExecutionError(_(f"Error while injecting servermodel {servermodel_description['name']} in database: {err}")) + return {'retcode': 0, 'returns': _('Servermodels successfully loaded')} @register('v1.servermodel.list', None, database=True) - async def servermodel_list(self, risotto_context, sourceid): + async def servermodel_list(self, + risotto_context: Context, + sourceid: int): sql = ''' - SELECT * FROM ServerModel + SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id FROM Servermodel ''' servermodels = await risotto_context.connection.fetch(sql) return [dict(r) for r in servermodels]