add v1.servermodel.create message

This commit is contained in:
Emmanuel Garette 2020-01-31 11:23:18 +01:00
parent 6bdf21d1ac
commit 3b3cefa38a
9 changed files with 187 additions and 122 deletions

View File

@ -73,3 +73,8 @@ S=xxxxxxxxxxxxxxxxxxxxxx
# Create a new user and set role 'server_rw' for this server # Create a new user and set role 'server_rw' for this server
./script/cucchiaiata user.create -l gnunux -n gnunux -s gnunux ./script/cucchiaiata user.create -l gnunux -n gnunux -s gnunux
./script/cucchiaiata user.role.create -u gnunux -n 'server_rw' -a 'Server.ServerName' -v test ./script/cucchiaiata user.role.create -u gnunux -n 'server_rw' -a 'Server.ServerName' -v test
# Heritage
./script/cucchiaiata servermodel.create -n aca -d Aca -p eolebase -s eole -r last
./script/cucchiaiata session.servermodel.start -s aca
./script/cucchiaiata session.servermodel.configure -s $S --creole.reseau.unbound_domain_name test.cadoles.com

View File

@ -11,16 +11,6 @@ parameters:
ref: Servermodel.ServermodelName ref: Servermodel.ServermodelName
shortarg: s shortarg: s
description: Nom du serveurmodel. description: Nom du serveurmodel.
source_name:
type: String
shortarg: n
description: Nom de la source.
ref: Source.SourceName
release_distribution:
type: String
shortarg: r
description: Nom de la distribution.
ref: Source.ReleaseDistribution
response: response:
type: Session type: Session

View File

@ -23,8 +23,13 @@ properties:
type: number type: number
ref: Servermodel.SubreleaseId ref: Servermodel.SubreleaseId
description: Version du modèle de serveur. description: Version du modèle de serveur.
servermodel_applicationservice_id:
type: number
ref: Applicationservice.ApplicationserviceId
description: Identifiant de l'application service local.
required: required:
- servermodel_id - servermodel_id
- servermodel_name - servermodel_name
- servermodel_description - servermodel_description
- release_id - release_id
- servermodel_applicationservice_id

View File

@ -26,6 +26,8 @@ def get_config():
'rougail_dtd_path': '../rougail/data/creole.dtd', 'rougail_dtd_path': '../rougail/data/creole.dtd',
'admin_user': DEFAULT_USER}, 'admin_user': DEFAULT_USER},
'source': {'root_path': '/srv/seed'}, 'source': {'root_path': '/srv/seed'},
'cache': {'root_path': '/var/cache/risotto'} 'cache': {'root_path': '/var/cache/risotto'},
'servermodel': {'internal_source': 'internal',
'internal_distribution': 'last'},
} }

View File

@ -87,21 +87,39 @@ class CallDispatcher:
kwargs, kwargs,
function_objs) function_objs)
else: else:
async with self.pool.acquire() as connection: try:
await connection.set_type_codec( async with self.pool.acquire() as connection:
'json', await connection.set_type_codec(
encoder=dumps, 'json',
decoder=loads, encoder=dumps,
schema='pg_catalog' decoder=loads,
) schema='pg_catalog'
risotto_context.connection = connection )
async with connection.transaction(): risotto_context.connection = connection
return await self.launch(version, async with connection.transaction():
message, return await self.launch(version,
risotto_context, message,
check_role, risotto_context,
kwargs, check_role,
function_objs) kwargs,
function_objs)
except CallError as err:
raise err
except Exception as err:
# if there is a problem with arguments, just send an error and do nothing
if get_config()['global']['debug']:
print_exc()
async with self.pool.acquire() as connection:
await connection.set_type_codec(
'json',
encoder=dumps,
decoder=loads,
schema='pg_catalog'
)
risotto_context.connection = connection
async with connection.transaction():
await log.error_msg(risotto_context, kwargs, err)
raise err
class PublishDispatcher: class PublishDispatcher:
@ -126,21 +144,39 @@ class PublishDispatcher:
kwargs, kwargs,
function_objs) function_objs)
else: else:
async with self.pool.acquire() as connection: try:
await connection.set_type_codec( async with self.pool.acquire() as connection:
'json', await connection.set_type_codec(
encoder=dumps, 'json',
decoder=loads, encoder=dumps,
schema='pg_catalog' decoder=loads,
) schema='pg_catalog'
risotto_context.connection = connection )
async with connection.transaction(): risotto_context.connection = connection
return await self.launch(version, async with connection.transaction():
message, return await self.launch(version,
risotto_context, message,
check_role, risotto_context,
kwargs, check_role,
function_objs) kwargs,
function_objs)
except CallError as err:
raise err
except Exception as err:
# if there is a problem with arguments, just send an error and do nothing
if get_config()['global']['debug']:
print_exc()
async with self.pool.acquire() as connection:
await connection.set_type_codec(
'json',
encoder=dumps,
decoder=loads,
schema='pg_catalog'
)
risotto_context.connection = connection
async with connection.transaction():
await log.error_msg(risotto_context, kwargs, err)
raise err
class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher): class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher):
@ -267,19 +303,10 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
function_objs: List) -> Optional[Dict]: function_objs: List) -> Optional[Dict]:
await self.check_message_type(risotto_context, await self.check_message_type(risotto_context,
kwargs) kwargs)
try: config_arguments = await self.load_kwargs_to_config(risotto_context,
config_arguments = await self.load_kwargs_to_config(risotto_context, f'{version}.{message}',
f'{version}.{message}', kwargs,
kwargs, check_role)
check_role)
except Exception as err:
# if there is a problem with arguments, just send an error and do nothing
if get_config()['global']['debug']:
print_exc()
await log.error_msg(risotto_context, kwargs, err)
if risotto_context.type == 'rpc':
raise err
return
# config is ok, so send the message # config is ok, so send the message
for function_obj in function_objs: for function_obj in function_objs:
function = function_obj['function'] function = function_obj['function']
@ -305,13 +332,13 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
raise err raise err
continue continue
except Exception as err: except Exception as err:
if risotto_context.type == 'rpc':
raise err
if get_config().get('global').get('debug'): if get_config().get('global').get('debug'):
print_exc() print_exc()
await log.error_msg(risotto_context, await log.error_msg(risotto_context,
kwargs, kwargs,
err) err)
if risotto_context.type == 'rpc':
raise CallError(str(err))
continue continue
else: else:
if risotto_context.type == 'rpc': if risotto_context.type == 'rpc':

View File

@ -17,8 +17,10 @@ from ...logger import log
class Risotto(Controller): class Risotto(Controller):
def __init__(self, def __init__(self,
test: bool) -> None: test: bool) -> None:
self.source_root_path = get_config().get('source').get('root_path') self.source_root_path = get_config()['source']['root_path']
self.cache_root_path = join(get_config().get('cache').get('root_path'), 'servermodel') self.cache_root_path = join(get_config()['cache']['root_path'], 'servermodel')
self.internal_source_name = get_config()['servermodel']['internal_source']
self.internal_distribution_name = get_config()['servermodel']['internal_distribution']
if not isdir(self.cache_root_path): if not isdir(self.cache_root_path):
makedirs(join(self.cache_root_path)) makedirs(join(self.cache_root_path))
@ -26,13 +28,13 @@ class Risotto(Controller):
risotto_context: Context) -> None: risotto_context: Context) -> None:
internal_source = await self.call('v1.source.create', internal_source = await self.call('v1.source.create',
risotto_context, risotto_context,
source_name='internal', source_name=self.internal_source_name,
source_url='none') source_url='none')
internal_release = await self.call('v1.source.release.create', internal_release = await self.call('v1.source.release.create',
risotto_context, risotto_context,
source_name='internal', source_name=self.internal_source_name,
release_name='none', release_name='none',
release_distribution='last') release_distribution=self.internal_distribution_name)
self.internal_release_id = internal_release['release_id'] self.internal_release_id = internal_release['release_id']
async def servermodel_gen_funcs(self, async def servermodel_gen_funcs(self,
@ -158,16 +160,20 @@ class Risotto(Controller):
risotto_context: Context, risotto_context: Context,
servermodel_name: str, servermodel_name: str,
servermodel_description: str, servermodel_description: str,
servermodel_parents_id: List[int], servermodel_parents: List[Dict],
dependencies: List[int], dependencies: List[int],
release_id: int, release_id: int,
release_cache: Dict=None) -> Dict: release_cache: Dict=None) -> Dict:
servermodel_update = """INSERT INTO Servermodel(ServermodelName, ServermodelDescription, ServermodelParentsId, ServermodelReleaseId, ServermodelApplicationServiceId) servermodel_insert = """INSERT INTO Servermodel(ServermodelName, ServermodelDescription, ServermodelParentsId, ServermodelReleaseId, ServermodelApplicationServiceId)
VALUES ($1,$2,$3,$4,$5) VALUES ($1,$2,$3,$4,$5)
RETURNING ServermodelId RETURNING ServermodelId
""" """
as_name = f"local_{servermodel_name}" as_name = f"local_{servermodel_name}"
as_description = f'local application service for {servermodel_name}' as_description = f'local application service for {servermodel_name}'
servermodel_parents_id = []
for servermodel_parent in servermodel_parents:
servermodel_parents_id.append(servermodel_parent['servermodel_id'])
dependencies.append(servermodel_parent['servermodel_applicationservice_id'])
applicationservice = await self.call('v1.applicationservice.create', applicationservice = await self.call('v1.applicationservice.create',
risotto_context, risotto_context,
applicationservice_name=as_name, applicationservice_name=as_name,
@ -175,7 +181,7 @@ class Risotto(Controller):
applicationservice_dependencies=dependencies, applicationservice_dependencies=dependencies,
release_id=self.internal_release_id) release_id=self.internal_release_id)
applicationservice_id = applicationservice['applicationservice_id'] applicationservice_id = applicationservice['applicationservice_id']
servermodel_id = await risotto_context.connection.fetchval(servermodel_update, servermodel_id = await risotto_context.connection.fetchval(servermodel_insert,
servermodel_name, servermodel_name,
servermodel_description, servermodel_description,
servermodel_parents_id, servermodel_parents_id,
@ -191,7 +197,7 @@ class Risotto(Controller):
# build cache to have all release informations # build cache to have all release informations
if release_cache is None: if release_cache is None:
release_cache = {} release_cache = {}
for applicationservice_id, applicationservice_infos in dependencies.items(): for applicationservice_infos in dependencies.values():
applicationservice_name, as_release_id = applicationservice_infos applicationservice_name, as_release_id = applicationservice_infos
if as_release_id not in release_cache: if as_release_id not in release_cache:
release_cache[as_release_id] = await self.call('v1.source.release.get_by_id', release_cache[as_release_id] = await self.call('v1.source.release.get_by_id',
@ -216,11 +222,9 @@ class Risotto(Controller):
sm_dict = {'servermodel_name': servermodel_name, sm_dict = {'servermodel_name': servermodel_name,
'servermodel_description': servermodel_description, 'servermodel_description': servermodel_description,
'servermodel_parents_id': servermodel_parents_id, 'servermodel_parents_id': servermodel_parents_id,
'servermodel_applicationservice_id': applicationservice_id,
'release_id': release_id, 'release_id': release_id,
'servermodel_id': servermodel_id} 'servermodel_id': servermodel_id}
await self.publish('v1.servermodel.created',
risotto_context,
**sm_dict)
return sm_dict return sm_dict
def parse_parents(self, def parse_parents(self,
@ -235,15 +239,6 @@ class Risotto(Controller):
self.parse_parents(servermodels, servermodels[parent], parents) self.parse_parents(servermodels, servermodels[parent], parents)
return 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') @register('v1.servermodel.dataset.updated')
async def servermodel_update(self, async def servermodel_update(self,
risotto_context: Context, risotto_context: Context,
@ -284,16 +279,15 @@ class Risotto(Controller):
parents = self.parse_parents(servermodels, parents = self.parse_parents(servermodels,
servermodel) servermodel)
parents.reverse() parents.reverse()
servermodelparent_id = [] servermodel_parent = []
for new_servermodel in parents: for new_servermodel in parents:
if not servermodels[new_servermodel]['done']: if not servermodels[new_servermodel]['done']:
servermodel_description = servermodels[new_servermodel] servermodel_description = servermodels[new_servermodel]
parent = servermodel_description['parent'] parent = servermodel_description['parent']
if not servermodelparent_id and parent is not None: if not servermodel_parent and parent is not None:
# parent is a str, so get ID servermodel_parent = [await self._servermodel_describe(risotto_context,
servermodelparent_id = [await self.get_servermodel_id_by_name(risotto_context, parent,
parent, release_id)]
release_id)]
# link application service with this servermodel # link application service with this servermodel
dependencies = [] dependencies = []
for depend in servermodels[new_servermodel]['applicationservices']: for depend in servermodels[new_servermodel]['applicationservices']:
@ -308,16 +302,18 @@ class Risotto(Controller):
servermodel_ob = await self._servermodel_create(risotto_context, servermodel_ob = await self._servermodel_create(risotto_context,
sm_name, sm_name,
sm_description, sm_description,
servermodelparent_id, servermodel_parent,
dependencies, dependencies,
release_id, release_id,
release_cache) release_cache)
servermodel_id = servermodel_ob['servermodel_id'] await self.publish('v1.servermodel.created',
risotto_context,
**servermodel_ob)
except Exception as err: except Exception as err:
if get_config().get('global').get('debug'): if get_config().get('global').get('debug'):
print_exc() print_exc()
raise ExecutionError(_(f"Error while injecting servermodel {sm_name} in database: {err}")) raise ExecutionError(_(f"Error while injecting servermodel {sm_name} in database: {err}"))
servermodelparent_id = [servermodel_id] servermodel_parent = [servermodel_ob]
servermodel_description['done'] = True servermodel_description['done'] = True
return {'retcode': 0, 'returns': _('Servermodels successfully loaded')} return {'retcode': 0, 'returns': _('Servermodels successfully loaded')}
@ -326,7 +322,7 @@ class Risotto(Controller):
risotto_context: Context, risotto_context: Context,
source_id: int): source_id: int):
sql = ''' sql = '''
SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id, ServermodelApplicationServiceId as servermodel_applicationservice_id
FROM Servermodel FROM Servermodel
''' '''
servermodels = await risotto_context.connection.fetch(sql) servermodels = await risotto_context.connection.fetch(sql)
@ -342,24 +338,58 @@ class Risotto(Controller):
risotto_context, risotto_context,
source_name=source_name, source_name=source_name,
release_distribution=release_distribution) release_distribution=release_distribution)
return await self._servermodel_describe(risotto_context,
servermodel_name,
release['release_id'])
async def _servermodel_describe(self,
risotto_context,
servermodel_name,
release_id):
sql = ''' sql = '''
SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id, ServermodelApplicationServiceId as servermodel_applicationservice_id
FROM Servermodel FROM Servermodel
WHERE ServermodelName=$1 AND ServermodelReleaseId=$2 WHERE ServermodelName=$1 AND ServermodelReleaseId=$2
''' '''
servermodel = await risotto_context.connection.fetchrow(sql, servermodel = await risotto_context.connection.fetchrow(sql,
servermodel_name, servermodel_name,
release['release_id']) release_id)
if not servermodel: if not servermodel:
raise Exception(_(f'{servermodel_id} is not a valid ID for a servermodel')) raise Exception(_(f'"{servermodel_name}" is not a valid name for a servermodel in source "{source_name}" and release "{release_distribution}"'))
return dict(servermodel) return dict(servermodel)
@register('v1.servermodel.create', notification='v1.servermodel.created')
async def create_servermodel(self,
risotto_context: Context,
servermodel_name: str,
servermodel_description: str,
servermodel_parents_name: List[int],
servermodel_parents_source_name: str,
servermodel_parents_release_distribution: str) -> Dict:
release = await self.call('v1.source.release.describe',
risotto_context,
source_name=servermodel_parents_source_name,
release_distribution=servermodel_parents_release_distribution)
release_id = release['release_id']
servermodel_parents = []
for servermodel_parent_name in servermodel_parents_name:
servermodel_parents.append(await self._servermodel_describe(risotto_context,
servermodel_parent_name,
release_id))
return await self._servermodel_create(risotto_context,
servermodel_name,
servermodel_description,
servermodel_parents,
[],
self.internal_release_id,
{})
@register('v1.servermodel.get_by_id') @register('v1.servermodel.get_by_id')
async def servermodel_get_by_id(self, async def servermodel_get_by_id(self,
risotto_context: Context, risotto_context: Context,
servermodel_id: int) -> Dict: servermodel_id: int) -> Dict:
sql = ''' sql = '''
SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id, ServermodelApplicationServiceId as servermodel_applicationservice_id
FROM Servermodel FROM Servermodel
WHERE ServermodelId=$1 WHERE ServermodelId=$1
''' '''

View File

@ -12,12 +12,15 @@ from .storage import storage_server, storage_servermodel
from ...controller import Controller from ...controller import Controller
from ...register import register from ...register import register
from ...dispatcher import dispatcher from ...dispatcher import dispatcher
from ...config import get_config
class Risotto(Controller): class Risotto(Controller):
def __init__(self, def __init__(self,
test): test):
self.modify_storage = Storage(engine='dictionary') self.modify_storage = Storage(engine='dictionary')
self.internal_source_name = get_config()['servermodel']['internal_source']
self.internal_distribution_name = get_config()['servermodel']['internal_distribution']
def get_storage(self, def get_storage(self,
type: str): type: str):
@ -110,17 +113,15 @@ class Risotto(Controller):
@register('v1.session.servermodel.start') @register('v1.session.servermodel.start')
async def start_session_servermodel(self, async def start_session_servermodel(self,
risotto_context: Context, risotto_context: Context,
servermodel_name: str, servermodel_name: str) -> Dict:
source_name: str,
release_distribution: str) -> Dict:
""" start a new config session for a server or a servermodel """ start a new config session for a server or a servermodel
""" """
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
servermodel = await self.call('v1.servermodel.describe', servermodel = await self.call('v1.servermodel.describe',
risotto_context, risotto_context,
servermodel_name=servermodel_name, servermodel_name=servermodel_name,
source_name=source_name, source_name=self.internal_source_name,
release_distribution=release_distribution) release_distribution=self.internal_distribution_name)
if not servermodel or servermodel['servermodel_id'] not in config_module.servermodel: if not servermodel or servermodel['servermodel_id'] not in config_module.servermodel:
raise Exception(_(f'cannot find servermodel with name {servermodel_name}')) raise Exception(_(f'cannot find servermodel with name {servermodel_name}'))
id = servermodel['servermodel_id'] id = servermodel['servermodel_id']

View File

@ -21,41 +21,45 @@ class Risotto(Controller):
async def template_get(self, async def template_get(self,
risotto_context, risotto_context,
server_name: str) -> Dict: server_name: str) -> Dict:
# get informations for server
server = await self.call('v1.server.describe', server = await self.call('v1.server.describe',
risotto_context, risotto_context,
server_name=server_name) server_name=server_name)
server_id = server['server_id'] server_id = server['server_id']
servermodel_id = server['server_servermodel_id'] servermodel_id = server['server_servermodel_id']
# verify if server has deployed configuration
config_module = dispatcher.get_service('config') config_module = dispatcher.get_service('config')
server = config_module.server[server_id] server = config_module.server[server_id]
export = await server['server'].value.exportation() export = await server['server'].value.exportation()
if not export[0]: if not export[0]:
raise Exception(_(f'configuration for server "{server_name}" is empty, you should deploy it first')) raise Exception(_(f'configuration for server "{server_name}" is empty, you should deploy it first'))
config = meta = await server['server'].config.deepcopy(storage=self.storage) # copy deployed configuration
while True: async with await server['server'].config.deepcopy(storage=self.storage) as config:
try: meta = config
children = list(await config.config.list()) while True:
except: try:
break children = list(await config.config.list())
if children: except:
config = children[0] break
else: if children:
break config = children[0]
configurations_dir = join(CONFIGURATION_DIR, else:
str(server_id)) break
if isdir(configurations_dir): configurations_dir = join(CONFIGURATION_DIR,
rmtree(configurations_dir) str(server_id))
mkdir(configurations_dir) if isdir(configurations_dir):
tmp_dir = join(TMP_DIR, str(server_id)) rmtree(configurations_dir)
if isdir(tmp_dir): mkdir(configurations_dir)
rmtree(tmp_dir) tmp_dir = join(TMP_DIR, str(server_id))
mkdir(tmp_dir) if isdir(tmp_dir):
templates_dir = join(self.cache_root_path, str(servermodel_id), 'templates') rmtree(tmp_dir)
await generate(config, mkdir(tmp_dir)
server['funcs_file'], templates_dir = join(self.cache_root_path, str(servermodel_id), 'templates')
templates_dir, await generate(config,
tmp_dir, server['funcs_file'],
configurations_dir) templates_dir,
tmp_dir,
configurations_dir)
del meta
return {'server_name': server_name, return {'server_name': server_name,
'template_dir': configurations_dir} 'template_dir': configurations_dir}

View File

@ -12,6 +12,7 @@ class Risotto(Controller):
for uri in ['v1.applicationservice.create', for uri in ['v1.applicationservice.create',
'v1.applicationservice.dataset.updated', 'v1.applicationservice.dataset.updated',
'v1.server.create', 'v1.server.create',
'v1.servermodel.create',
'v1.servermodel.dataset.updated', 'v1.servermodel.dataset.updated',
'v1.session.server.start', 'v1.session.server.start',
'v1.source.create', 'v1.source.create',