WIP load servermodel with parent
This commit is contained in:
parent
eccc5c4098
commit
b567fd88ac
|
@ -2,6 +2,7 @@ from os import listdir
|
||||||
from os.path import join
|
from os.path import join
|
||||||
from yaml import load, SafeLoader
|
from yaml import load, SafeLoader
|
||||||
from traceback import print_exc
|
from traceback import print_exc
|
||||||
|
from typing import Dict, List
|
||||||
from ...controller import Controller
|
from ...controller import Controller
|
||||||
from ...register import register
|
from ...register import register
|
||||||
from ...utils import _
|
from ...utils import _
|
||||||
|
@ -11,15 +12,63 @@ from ...error import ExecutionError
|
||||||
|
|
||||||
|
|
||||||
class Risotto(Controller):
|
class Risotto(Controller):
|
||||||
|
async def _servermodel_create(self,
|
||||||
|
risotto_context: Context,
|
||||||
|
servermodel_name: str,
|
||||||
|
servermodel_description: str,
|
||||||
|
servermodel_parents_id,
|
||||||
|
release_id):
|
||||||
|
servermodel_update = """INSERT INTO Servermodel(ServermodelName, ServermodelDescription, ServermodelParentsId, ServermodelReleaseId, ServermodelApplicationServiceId) VALUES ($1,$2,$3,$4,$5)
|
||||||
|
RETURNING ServermodelId
|
||||||
|
"""
|
||||||
|
as_name = f"local_{servermodel_name}"
|
||||||
|
as_description = f'local application service for {servermodel_name}'
|
||||||
|
applicationservice = await self.call('v1.applicationservice.create',
|
||||||
|
risotto_context,
|
||||||
|
applicationservice_name=as_name,
|
||||||
|
applicationservice_description=as_description,
|
||||||
|
release_id=release_id)
|
||||||
|
servermodel = await risotto_context.connection.fetchval(servermodel_update,
|
||||||
|
sm_name,
|
||||||
|
sm_description,
|
||||||
|
sm_parentsid,
|
||||||
|
release_id,
|
||||||
|
applicationservice['applicationservice_id'])
|
||||||
|
return {'servermodel_name': servermodel_name,
|
||||||
|
'servermodel_description': servermodel_description,
|
||||||
|
'servermodel_parents_id': servermodel_parents_id,
|
||||||
|
'release_id': release_id,
|
||||||
|
'servermodel_id': servermodel['servermodel_id']}
|
||||||
|
|
||||||
|
def parse_parents(self,
|
||||||
|
servermodels: Dict,
|
||||||
|
servermodel: Dist,
|
||||||
|
parents: List=None) -> List:
|
||||||
|
if parents is None:
|
||||||
|
parents = [servermodel]
|
||||||
|
parent = servermodels[servermodel]['parent']
|
||||||
|
if parent in servermodels:
|
||||||
|
parents.append(parent)
|
||||||
|
self.parse_parents(servermodels, parent, parents)
|
||||||
|
return parents
|
||||||
|
|
||||||
|
async def get_servermodel_id_by_name(self,
|
||||||
|
risotto_context: Context,
|
||||||
|
servermodel_name: str,
|
||||||
|
release_id: int):
|
||||||
|
sql = 'SELECT ServermodelId as servermodel_id FROM Servermodel WHERE ServermodelName = $1 AND ServermodelReleaseId = $2',
|
||||||
|
return await risotto_context.connection.fetchval(sql,
|
||||||
|
servermodel_name,
|
||||||
|
release_id)['servermodel_id']
|
||||||
|
|
||||||
@register('v1.servermodel.dataset.updated', None, database=True)
|
@register('v1.servermodel.dataset.updated', None, database=True)
|
||||||
async def servermodel_update(self,
|
async def servermodel_update(self,
|
||||||
risotto_context: Context,
|
risotto_context: Context,
|
||||||
release_path: str,
|
release_path: str,
|
||||||
release_id: int):
|
release_id: int):
|
||||||
servermodel_update = """INSERT INTO Servermodel(ServermodelName, ServermodelDescription, ServermodelParentsId, ServermodelReleaseId, ServermodelApplicationServiceId) VALUES ($1,$2,$3,$4,$5)
|
|
||||||
RETURNING ServermodelId
|
|
||||||
"""
|
|
||||||
servermodel_path = join(release_path, 'servermodel')
|
servermodel_path = join(release_path, 'servermodel')
|
||||||
|
servermodels = {}
|
||||||
|
# for dependencies reason load all servermodels
|
||||||
for servermodel in listdir(servermodel_path):
|
for servermodel in listdir(servermodel_path):
|
||||||
if not servermodel.endswith('.yml'):
|
if not servermodel.endswith('.yml'):
|
||||||
continue
|
continue
|
||||||
|
@ -32,31 +81,38 @@ class Risotto(Controller):
|
||||||
if get_config().get('global').get('debug'):
|
if get_config().get('global').get('debug'):
|
||||||
print_exc()
|
print_exc()
|
||||||
raise ExecutionError(_(f'Error while reading {servermodel_description_path}: {err}'))
|
raise ExecutionError(_(f'Error while reading {servermodel_description_path}: {err}'))
|
||||||
sm_name = servermodel_description['name']
|
servermodels[servermodel_yml['name']] = servermodel_description
|
||||||
sm_description = servermodel_description['description']
|
servermodels[servermodel_yml['name']]['done'] = False
|
||||||
sm_parentsid = servermodel_description['parent']
|
|
||||||
if sm_parentsid is None:
|
for servermodel in servermodels:
|
||||||
sm_parentsid = []
|
if not servermodel['done']:
|
||||||
else:
|
# parent needs to create before child, so retrieve all parents
|
||||||
sm_parentsid = [sm_parentsid]
|
parents = self.parse_parents(servermodels,
|
||||||
as_name = f"local_{sm_name}"
|
servermodel)
|
||||||
as_description = f'local application service for {sm_name}'
|
parents.reverse()
|
||||||
applicationservice = await self.call('v1.applicationservice.create',
|
servermodelparent_id = []
|
||||||
risotto_context,
|
for new_servermodel in parents:
|
||||||
applicationservice_name=as_name,
|
if not servermodels[new_servermodel]['done']:
|
||||||
applicationservice_description=as_description,
|
servermodel_description = servermodels[new_servermodel]
|
||||||
release_id=release_id)
|
parent = servermodel_description['parent']
|
||||||
try:
|
if not servermodelparent_id and parent None:
|
||||||
await risotto_context.connection.fetch(servermodel_update,
|
# parent is a str, so get ID
|
||||||
sm_name,
|
servermodelparent_id = [await self.get_servermodel_id_by_name(risotto_context,
|
||||||
sm_description,
|
parent,
|
||||||
sm_parentsid,
|
release_id)]
|
||||||
release_id,
|
sm_name = servermodel_description['name']
|
||||||
applicationservice['applicationservice_id'])
|
sm_description = servermodel_description['description']
|
||||||
except Exception as err:
|
try:
|
||||||
if get_config().get('global').get('debug'):
|
servermodel_id = await self._servermodel_create(sm_name,
|
||||||
print_exc()
|
sm_description,
|
||||||
raise ExecutionError(_(f"Error while injecting servermodel {servermodel_description['name']} in database: {err}"))
|
servermodelparent_id,
|
||||||
|
release_id)['servermodel_id']
|
||||||
|
except Exception as err:
|
||||||
|
if get_config().get('global').get('debug'):
|
||||||
|
print_exc()
|
||||||
|
raise ExecutionError(_(f"Error while injecting servermodel {sm_name} in database: {err}"))
|
||||||
|
servermodelparent_id = [servermodel_id]
|
||||||
|
servermodel_description['done'] = True
|
||||||
return {'retcode': 0, 'returns': _('Servermodels successfully loaded')}
|
return {'retcode': 0, 'returns': _('Servermodels successfully loaded')}
|
||||||
|
|
||||||
@register('v1.servermodel.list', None, database=True)
|
@register('v1.servermodel.list', None, database=True)
|
||||||
|
|
Loading…
Reference in New Issue