Compare commits
2 Commits
5666c01bdc
...
3b3cefa38a
Author | SHA1 | Date | |
---|---|---|---|
3b3cefa38a | |||
6bdf21d1ac |
@ -44,6 +44,9 @@ su - postgres
|
||||
psql -U postgres risotto
|
||||
drop table log; drop table userrole; drop table release; drop table source; drop table server; drop table servermodel; drop table applicationservice; drop table roleuri; drop table risottouser; drop table uri;
|
||||
|
||||
psql -U postgres tiramisu
|
||||
drop table value; drop table property; drop table permissive; drop table information; drop table session;
|
||||
|
||||
# Import EOLE
|
||||
./script/cucchiaiata source.create -n eole -u http://localhost
|
||||
./script/cucchiaiata source.release.create -s eole -n 2.7.1.1 -d last
|
||||
@ -70,3 +73,8 @@ S=xxxxxxxxxxxxxxxxxxxxxx
|
||||
# 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.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
|
||||
|
@ -15,6 +15,9 @@ services:
|
||||
#command: tail -F /var/log
|
||||
command: python -u /srv/src/risotto/script/server.py
|
||||
restart: on-failure
|
||||
environment:
|
||||
RISOTTO_DSN: ${RISOTTO_DSN:-postgres://risotto:risotto@postgres:5432/risotto}
|
||||
RISOTTO_TIRAMISU_DSN: ${RISOTTO_TIRAMISU_DSN:-postgres://risotto:risotto@postgres:5432/tiramisu}
|
||||
postgres:
|
||||
image: postgres:11-alpine
|
||||
environment:
|
||||
@ -25,4 +28,4 @@ services:
|
||||
- ./postgres-init/:/docker-entrypoint-initdb.d/
|
||||
ports:
|
||||
- "5432:5432"
|
||||
restart: unless-stopped
|
||||
restart: unless-stopped
|
||||
|
@ -11,16 +11,6 @@ parameters:
|
||||
ref: Servermodel.ServermodelName
|
||||
shortarg: s
|
||||
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:
|
||||
type: Session
|
||||
|
@ -23,8 +23,13 @@ properties:
|
||||
type: number
|
||||
ref: Servermodel.SubreleaseId
|
||||
description: Version du modèle de serveur.
|
||||
servermodel_applicationservice_id:
|
||||
type: number
|
||||
ref: Applicationservice.ApplicationserviceId
|
||||
description: Identifiant de l'application service local.
|
||||
required:
|
||||
- servermodel_id
|
||||
- servermodel_name
|
||||
- servermodel_description
|
||||
- release_id
|
||||
- servermodel_applicationservice_id
|
||||
|
@ -96,16 +96,8 @@ CREATE TABLE log(
|
||||
"""
|
||||
|
||||
async def main():
|
||||
db_conf = get_config().get('database')
|
||||
#asyncpg.connect('postgresql://postgres@localhost/test')
|
||||
engine = db_conf.get('engine')
|
||||
host = db_conf.get('host')
|
||||
dbname = db_conf.get('dbname')
|
||||
dbuser = db_conf.get('user')
|
||||
dbpassword = db_conf.get('password')
|
||||
dbport = db_conf.get('port')
|
||||
cfg = "{}://{}:{}@{}:{}/{}".format(engine, dbuser, dbpassword, host, dbport, dbname)
|
||||
pool = await asyncpg.create_pool(cfg)
|
||||
db_conf = get_config()['database']['dsn']
|
||||
pool = await asyncpg.create_pool(db_conf)
|
||||
async with pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
returns = await connection.execute(VERSION_INIT)
|
||||
|
@ -1,6 +1,7 @@
|
||||
from asyncio import get_event_loop
|
||||
from risotto import get_app
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
loop = get_event_loop()
|
||||
loop.run_until_complete(get_app(loop))
|
||||
@ -8,5 +9,3 @@ if __name__ == '__main__':
|
||||
loop.run_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
|
||||
|
@ -1,23 +1,20 @@
|
||||
MESSAGE_ROOT_PATH = 'messages'
|
||||
DATABASE_DIR = '/var/cache/risotto/database'
|
||||
INTERNAL_USER = 'internal'
|
||||
CONFIGURATION_DIR = 'configurations'
|
||||
TEMPLATE_DIR = 'templates'
|
||||
TMP_DIR = 'tmp'
|
||||
ROUGAIL_DTD_PATH = '../rougail/data/creole.dtd'
|
||||
DEFAULT_USER = 'Anonymous'
|
||||
DEFAULT_DSN = 'postgres:///risotto?host=/var/run/postgresql/&user=risotto'
|
||||
DEFAULT_TIRAMISU_DSN = 'postgres:///tiramisu?host=/var/run/postgresql/&user=tiramisu'
|
||||
|
||||
import os
|
||||
from os import environ
|
||||
from pathlib import PurePosixPath
|
||||
CURRENT_PATH = PurePosixPath(__file__)
|
||||
|
||||
def get_config():
|
||||
return {'database': {'engine': 'postgres',
|
||||
'host': 'postgres',
|
||||
'port': 5432,
|
||||
'dbname': 'risotto',
|
||||
'user': 'risotto',
|
||||
'password': 'risotto',
|
||||
return {'database': {'dsn': environ.get('RISOTTO_DSN', DEFAULT_DSN),
|
||||
'tiramisu_dsn': environ.get('RISOTTO_TIRAMISU_DSN', DEFAULT_TIRAMISU_DSN),
|
||||
},
|
||||
'http_server': {'port': 8080,
|
||||
#'default_user': "gnunux"},
|
||||
@ -29,6 +26,8 @@ def get_config():
|
||||
'rougail_dtd_path': '../rougail/data/creole.dtd',
|
||||
'admin_user': DEFAULT_USER},
|
||||
'source': {'root_path': '/srv/seed'},
|
||||
'cache': {'root_path': '/var/cache/risotto'}
|
||||
'cache': {'root_path': '/var/cache/risotto'},
|
||||
'servermodel': {'internal_source': 'internal',
|
||||
'internal_distribution': 'last'},
|
||||
}
|
||||
|
||||
|
@ -38,31 +38,30 @@ class CallDispatcher:
|
||||
raise Exception('hu?')
|
||||
else:
|
||||
for ret in returns:
|
||||
config = await Config(response,
|
||||
display_name=lambda self, dyn_name: self.impl_getname())
|
||||
await config.property.read_write()
|
||||
try:
|
||||
for key, value in ret.items():
|
||||
await config.option(key).value.set(value)
|
||||
except AttributeError:
|
||||
err = _(f'function {module_name}.{function_name} return the unknown parameter "{key}"')
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
except ValueError:
|
||||
err = _(f'function {module_name}.{function_name} return the parameter "{key}" with an unvalid value "{value}"')
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
await config.property.read_only()
|
||||
mandatories = await config.value.mandatory()
|
||||
if mandatories:
|
||||
mand = [mand.split('.')[-1] for mand in mandatories]
|
||||
raise ValueError(_(f'missing parameters in response: {mand} in message "{risotto_context.message}"'))
|
||||
try:
|
||||
await config.value.dict()
|
||||
except Exception as err:
|
||||
err = _(f'function {module_name}.{function_name} return an invalid response {err}')
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
async with await Config(response, display_name=lambda self, dyn_name: self.impl_getname()) as config:
|
||||
await config.property.read_write()
|
||||
try:
|
||||
for key, value in ret.items():
|
||||
await config.option(key).value.set(value)
|
||||
except AttributeError:
|
||||
err = _(f'function {module_name}.{function_name} return the unknown parameter "{key}"')
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
except ValueError:
|
||||
err = _(f'function {module_name}.{function_name} return the parameter "{key}" with an unvalid value "{value}"')
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
await config.property.read_only()
|
||||
mandatories = await config.value.mandatory()
|
||||
if mandatories:
|
||||
mand = [mand.split('.')[-1] for mand in mandatories]
|
||||
raise ValueError(_(f'missing parameters in response: {mand} in message "{risotto_context.message}"'))
|
||||
try:
|
||||
await config.value.dict()
|
||||
except Exception as err:
|
||||
err = _(f'function {module_name}.{function_name} return an invalid response {err}')
|
||||
await log.error_msg(risotto_context, kwargs, err)
|
||||
raise CallError(str(err))
|
||||
|
||||
async def call(self,
|
||||
version: str,
|
||||
@ -88,21 +87,39 @@ class CallDispatcher:
|
||||
kwargs,
|
||||
function_objs)
|
||||
else:
|
||||
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():
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
check_role,
|
||||
kwargs,
|
||||
function_objs)
|
||||
try:
|
||||
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():
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
check_role,
|
||||
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:
|
||||
@ -127,21 +144,39 @@ class PublishDispatcher:
|
||||
kwargs,
|
||||
function_objs)
|
||||
else:
|
||||
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():
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
check_role,
|
||||
kwargs,
|
||||
function_objs)
|
||||
try:
|
||||
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():
|
||||
return await self.launch(version,
|
||||
message,
|
||||
risotto_context,
|
||||
check_role,
|
||||
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):
|
||||
@ -182,31 +217,31 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
|
||||
""" create a new Config et set values to it
|
||||
"""
|
||||
# create a new config
|
||||
config = await Config(self.option)
|
||||
await config.property.read_write()
|
||||
# set message's option
|
||||
await config.option('message').value.set(risotto_context.message)
|
||||
# store values
|
||||
subconfig = config.option(risotto_context.message)
|
||||
for key, value in kwargs.items():
|
||||
try:
|
||||
await subconfig.option(key).value.set(value)
|
||||
except AttributeError:
|
||||
if get_config()['global']['debug']:
|
||||
print_exc()
|
||||
raise ValueError(_(f'unknown parameter in "{uri}": "{key}"'))
|
||||
# check mandatories options
|
||||
if check_role and get_config().get('global').get('check_role'):
|
||||
await self.check_role(subconfig,
|
||||
risotto_context.username,
|
||||
uri)
|
||||
await config.property.read_only()
|
||||
mandatories = await config.value.mandatory()
|
||||
if mandatories:
|
||||
mand = [mand.split('.')[-1] for mand in mandatories]
|
||||
raise ValueError(_(f'missing parameters in "{uri}": {mand}'))
|
||||
# return complete an validated kwargs
|
||||
return await subconfig.value.dict()
|
||||
async with await Config(self.option) as config:
|
||||
await config.property.read_write()
|
||||
# set message's option
|
||||
await config.option('message').value.set(risotto_context.message)
|
||||
# store values
|
||||
subconfig = config.option(risotto_context.message)
|
||||
for key, value in kwargs.items():
|
||||
try:
|
||||
await subconfig.option(key).value.set(value)
|
||||
except AttributeError:
|
||||
if get_config()['global']['debug']:
|
||||
print_exc()
|
||||
raise ValueError(_(f'unknown parameter in "{uri}": "{key}"'))
|
||||
# check mandatories options
|
||||
if check_role and get_config().get('global').get('check_role'):
|
||||
await self.check_role(subconfig,
|
||||
risotto_context.username,
|
||||
uri)
|
||||
await config.property.read_only()
|
||||
mandatories = await config.value.mandatory()
|
||||
if mandatories:
|
||||
mand = [mand.split('.')[-1] for mand in mandatories]
|
||||
raise ValueError(_(f'missing parameters in "{uri}": {mand}'))
|
||||
# return complete an validated kwargs
|
||||
return await subconfig.value.dict()
|
||||
|
||||
def get_service(self,
|
||||
name: str):
|
||||
@ -268,19 +303,10 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
|
||||
function_objs: List) -> Optional[Dict]:
|
||||
await self.check_message_type(risotto_context,
|
||||
kwargs)
|
||||
try:
|
||||
config_arguments = await self.load_kwargs_to_config(risotto_context,
|
||||
f'{version}.{message}',
|
||||
kwargs,
|
||||
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_arguments = await self.load_kwargs_to_config(risotto_context,
|
||||
f'{version}.{message}',
|
||||
kwargs,
|
||||
check_role)
|
||||
# config is ok, so send the message
|
||||
for function_obj in function_objs:
|
||||
function = function_obj['function']
|
||||
@ -306,13 +332,13 @@ class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher)
|
||||
raise err
|
||||
continue
|
||||
except Exception as err:
|
||||
if risotto_context.type == 'rpc':
|
||||
raise err
|
||||
if get_config().get('global').get('debug'):
|
||||
print_exc()
|
||||
await log.error_msg(risotto_context,
|
||||
kwargs,
|
||||
err)
|
||||
if risotto_context.type == 'rpc':
|
||||
raise CallError(str(err))
|
||||
continue
|
||||
else:
|
||||
if risotto_context.type == 'rpc':
|
||||
|
@ -1,7 +1,7 @@
|
||||
from aiohttp.web import Application, Response, get, post, HTTPBadRequest, HTTPInternalServerError, HTTPNotFound
|
||||
from json import dumps
|
||||
from traceback import print_exc
|
||||
from tiramisu import Config
|
||||
from tiramisu import Config, default_storage
|
||||
|
||||
|
||||
from .dispatcher import dispatcher
|
||||
@ -102,9 +102,9 @@ async def api(request, risotto_context):
|
||||
WHERE RoleURI.URIId = URI.URIId
|
||||
'''
|
||||
uris = [uri['uriname'] for uri in await connection.fetch(sql)]
|
||||
config = await Config(get_messages(load_shortarg=True, uris=uris)[1])
|
||||
await config.property.read_write()
|
||||
tiramisu = await config.option.dict(remotable='none')
|
||||
async with await Config(get_messages(load_shortarg=True, uris=uris)[1]) as config:
|
||||
await config.property.read_write()
|
||||
tiramisu = await config.option.dict(remotable='none')
|
||||
return tiramisu
|
||||
|
||||
|
||||
@ -119,6 +119,7 @@ async def get_app(loop):
|
||||
load_services()
|
||||
app = Application(loop=loop)
|
||||
routes = []
|
||||
default_storage.engine('dictionary')
|
||||
await dispatcher.load()
|
||||
for version, messages in dispatcher.messages.items():
|
||||
print()
|
||||
|
@ -59,13 +59,13 @@ class RegisterDispatcher:
|
||||
"""
|
||||
async def get_message_args():
|
||||
# load config
|
||||
config = await Config(self.option)
|
||||
await config.property.read_write()
|
||||
# set message to the uri name
|
||||
await config.option('message').value.set(message)
|
||||
# get message argument
|
||||
dico = await config.option(message).value.dict()
|
||||
return set(dico.keys())
|
||||
async with await Config(self.option) as config:
|
||||
await config.property.read_write()
|
||||
# set message to the uri name
|
||||
await config.option('message').value.set(message)
|
||||
# get message argument
|
||||
dico = await config.option(message).value.dict()
|
||||
return set(dico.keys())
|
||||
|
||||
def get_function_args():
|
||||
function_args = self.get_function_args(function)
|
||||
@ -101,13 +101,13 @@ class RegisterDispatcher:
|
||||
"""
|
||||
async def get_message_args():
|
||||
# load config
|
||||
config = await Config(self.option)
|
||||
await config.property.read_write()
|
||||
# set message to the message name
|
||||
await config.option('message').value.set(message)
|
||||
# get message argument
|
||||
dico = await config.option(message).value.dict()
|
||||
return set(dico.keys())
|
||||
async with await Config(self.option) as config:
|
||||
await config.property.read_write()
|
||||
# set message to the message name
|
||||
await config.option('message').value.set(message)
|
||||
# get message argument
|
||||
dico = await config.option(message).value.dict()
|
||||
return set(dico.keys())
|
||||
|
||||
def get_function_args():
|
||||
function_args = self.get_function_args(function)
|
||||
@ -247,16 +247,8 @@ class RegisterDispatcher:
|
||||
|
||||
async def load(self):
|
||||
# valid function's arguments
|
||||
db_conf = get_config().get('database')
|
||||
|
||||
engine = db_conf.get('engine')
|
||||
host = db_conf.get('host')
|
||||
dbname = db_conf.get('dbname')
|
||||
dbuser = db_conf.get('user')
|
||||
dbpassword = db_conf.get('password')
|
||||
dbport = db_conf.get('port')
|
||||
cfg = "{}://{}:{}@{}:{}/{}".format(engine, dbuser, dbpassword, host, dbport, dbname)
|
||||
self.pool = await asyncpg.create_pool(cfg)
|
||||
db_conf = get_config()['database']['dsn']
|
||||
self.pool = await asyncpg.create_pool(db_conf)
|
||||
async with self.pool.acquire() as connection:
|
||||
async with connection.transaction():
|
||||
for version, messages in self.messages.items():
|
||||
|
@ -10,7 +10,7 @@ from rougail import load as rougail_load
|
||||
|
||||
from ...controller import Controller
|
||||
from ...register import register
|
||||
from ...config import DATABASE_DIR, ROUGAIL_DTD_PATH, get_config
|
||||
from ...config import ROUGAIL_DTD_PATH, get_config
|
||||
from ...context import Context
|
||||
from ...utils import _
|
||||
from ...error import CallError, RegistrationError
|
||||
@ -23,11 +23,12 @@ class Risotto(Controller):
|
||||
test) -> None:
|
||||
global conf_storage
|
||||
self.cache_root_path = join(get_config().get('cache').get('root_path'), 'servermodel')
|
||||
for dirname in [self.cache_root_path, DATABASE_DIR]:
|
||||
if not isdir(dirname):
|
||||
raise RegistrationError(_(f'unable to find the cache dir "{dirname}"'))
|
||||
if not isdir(self.cache_root_path):
|
||||
raise RegistrationError(_(f'unable to find the cache dir "{self.cache_root_path}"'))
|
||||
if not test:
|
||||
self.save_storage = Storage(engine='sqlite3', dir_database=DATABASE_DIR)
|
||||
db_conf = get_config()['database']['tiramisu_dsn']
|
||||
self.save_storage = Storage(engine='postgres')
|
||||
self.save_storage.setting(dsn=db_conf)
|
||||
self.servermodel = {}
|
||||
self.server = {}
|
||||
super().__init__(test)
|
||||
@ -115,12 +116,10 @@ class Risotto(Controller):
|
||||
# build servermodel metaconfig (v_xxx.m_v_xxx)
|
||||
metaconfig = await MetaConfig([],
|
||||
optiondescription=optiondescription,
|
||||
persistent=True,
|
||||
session_id=session_id,
|
||||
storage=self.save_storage)
|
||||
mixconfig = await MixConfig(children=[],
|
||||
optiondescription=optiondescription,
|
||||
persistent=True,
|
||||
session_id='m_' + session_id,
|
||||
storage=self.save_storage)
|
||||
await metaconfig.config.add(mixconfig)
|
||||
@ -248,8 +247,7 @@ class Risotto(Controller):
|
||||
""" build server's config
|
||||
"""
|
||||
config = await metaconfig.config.new(session_id,
|
||||
storage=self.save_storage,
|
||||
persistent=True)
|
||||
storage=self.save_storage)
|
||||
await config.information.set('server_id', server_id)
|
||||
await config.information.set('server_name', server_name)
|
||||
await config.owner.set(server_name)
|
||||
|
@ -5,7 +5,6 @@ from yaml import load, SafeLoader
|
||||
from traceback import print_exc
|
||||
from typing import Dict, List, Optional
|
||||
from rougail import CreoleObjSpace
|
||||
from rougail.config import dtdfilename
|
||||
from ...controller import Controller
|
||||
from ...register import register
|
||||
from ...utils import _
|
||||
@ -18,8 +17,10 @@ from ...logger import log
|
||||
class Risotto(Controller):
|
||||
def __init__(self,
|
||||
test: bool) -> None:
|
||||
self.source_root_path = get_config().get('source').get('root_path')
|
||||
self.cache_root_path = join(get_config().get('cache').get('root_path'), 'servermodel')
|
||||
self.source_root_path = get_config()['source']['root_path']
|
||||
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):
|
||||
makedirs(join(self.cache_root_path))
|
||||
|
||||
@ -27,13 +28,13 @@ class Risotto(Controller):
|
||||
risotto_context: Context) -> None:
|
||||
internal_source = await self.call('v1.source.create',
|
||||
risotto_context,
|
||||
source_name='internal',
|
||||
source_name=self.internal_source_name,
|
||||
source_url='none')
|
||||
internal_release = await self.call('v1.source.release.create',
|
||||
risotto_context,
|
||||
source_name='internal',
|
||||
source_name=self.internal_source_name,
|
||||
release_name='none',
|
||||
release_distribution='last')
|
||||
release_distribution=self.internal_distribution_name)
|
||||
self.internal_release_id = internal_release['release_id']
|
||||
|
||||
async def servermodel_gen_funcs(self,
|
||||
@ -68,7 +69,7 @@ class Risotto(Controller):
|
||||
as_names_str = '", "'.join(as_names)
|
||||
await log.info(risotto_context,
|
||||
_(f'gen funcs for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
eolobj = CreoleObjSpace(dtdfilename)
|
||||
#eolobj = CreoleObjSpace(get_config()['global']['rougail_dtd_path'])
|
||||
|
||||
async def servermodel_gen_schema(self,
|
||||
servermodel_name: str,
|
||||
@ -106,7 +107,7 @@ class Risotto(Controller):
|
||||
continue
|
||||
as_names.add(applicationservice_name)
|
||||
extras.append((namespace, [extra_dir]))
|
||||
eolobj = CreoleObjSpace(dtdfilename)
|
||||
eolobj = CreoleObjSpace(get_config()['global']['rougail_dtd_path'])
|
||||
as_names_str = '", "'.join(as_names)
|
||||
await log.info(risotto_context,
|
||||
_(f'gen schema for "{servermodel_name}" with application services "{as_names_str}"'))
|
||||
@ -159,16 +160,20 @@ class Risotto(Controller):
|
||||
risotto_context: Context,
|
||||
servermodel_name: str,
|
||||
servermodel_description: str,
|
||||
servermodel_parents_id: List[int],
|
||||
servermodel_parents: List[Dict],
|
||||
dependencies: List[int],
|
||||
release_id: int,
|
||||
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)
|
||||
RETURNING ServermodelId
|
||||
"""
|
||||
as_name = f"local_{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',
|
||||
risotto_context,
|
||||
applicationservice_name=as_name,
|
||||
@ -176,7 +181,7 @@ class Risotto(Controller):
|
||||
applicationservice_dependencies=dependencies,
|
||||
release_id=self.internal_release_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_description,
|
||||
servermodel_parents_id,
|
||||
@ -192,7 +197,7 @@ class Risotto(Controller):
|
||||
# build cache to have all release informations
|
||||
if release_cache is None:
|
||||
release_cache = {}
|
||||
for applicationservice_id, applicationservice_infos in dependencies.items():
|
||||
for applicationservice_infos in dependencies.values():
|
||||
applicationservice_name, as_release_id = applicationservice_infos
|
||||
if as_release_id not in release_cache:
|
||||
release_cache[as_release_id] = await self.call('v1.source.release.get_by_id',
|
||||
@ -217,11 +222,9 @@ class Risotto(Controller):
|
||||
sm_dict = {'servermodel_name': servermodel_name,
|
||||
'servermodel_description': servermodel_description,
|
||||
'servermodel_parents_id': servermodel_parents_id,
|
||||
'servermodel_applicationservice_id': applicationservice_id,
|
||||
'release_id': release_id,
|
||||
'servermodel_id': servermodel_id}
|
||||
await self.publish('v1.servermodel.created',
|
||||
risotto_context,
|
||||
**sm_dict)
|
||||
return sm_dict
|
||||
|
||||
def parse_parents(self,
|
||||
@ -236,15 +239,6 @@ class Risotto(Controller):
|
||||
self.parse_parents(servermodels, 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')
|
||||
async def servermodel_update(self,
|
||||
risotto_context: Context,
|
||||
@ -285,16 +279,15 @@ class Risotto(Controller):
|
||||
parents = self.parse_parents(servermodels,
|
||||
servermodel)
|
||||
parents.reverse()
|
||||
servermodelparent_id = []
|
||||
servermodel_parent = []
|
||||
for new_servermodel in parents:
|
||||
if not servermodels[new_servermodel]['done']:
|
||||
servermodel_description = servermodels[new_servermodel]
|
||||
parent = servermodel_description['parent']
|
||||
if not servermodelparent_id and parent is not None:
|
||||
# parent is a str, so get ID
|
||||
servermodelparent_id = [await self.get_servermodel_id_by_name(risotto_context,
|
||||
parent,
|
||||
release_id)]
|
||||
if not servermodel_parent and parent is not None:
|
||||
servermodel_parent = [await self._servermodel_describe(risotto_context,
|
||||
parent,
|
||||
release_id)]
|
||||
# link application service with this servermodel
|
||||
dependencies = []
|
||||
for depend in servermodels[new_servermodel]['applicationservices']:
|
||||
@ -309,16 +302,18 @@ class Risotto(Controller):
|
||||
servermodel_ob = await self._servermodel_create(risotto_context,
|
||||
sm_name,
|
||||
sm_description,
|
||||
servermodelparent_id,
|
||||
servermodel_parent,
|
||||
dependencies,
|
||||
release_id,
|
||||
release_cache)
|
||||
servermodel_id = servermodel_ob['servermodel_id']
|
||||
await self.publish('v1.servermodel.created',
|
||||
risotto_context,
|
||||
**servermodel_ob)
|
||||
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_parent = [servermodel_ob]
|
||||
servermodel_description['done'] = True
|
||||
return {'retcode': 0, 'returns': _('Servermodels successfully loaded')}
|
||||
|
||||
@ -327,7 +322,7 @@ class Risotto(Controller):
|
||||
risotto_context: Context,
|
||||
source_id: int):
|
||||
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
|
||||
'''
|
||||
servermodels = await risotto_context.connection.fetch(sql)
|
||||
@ -343,24 +338,58 @@ class Risotto(Controller):
|
||||
risotto_context,
|
||||
source_name=source_name,
|
||||
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 = '''
|
||||
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
|
||||
WHERE ServermodelName=$1 AND ServermodelReleaseId=$2
|
||||
'''
|
||||
servermodel = await risotto_context.connection.fetchrow(sql,
|
||||
servermodel_name,
|
||||
release['release_id'])
|
||||
release_id)
|
||||
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)
|
||||
|
||||
@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')
|
||||
async def servermodel_get_by_id(self,
|
||||
risotto_context: Context,
|
||||
servermodel_id: int) -> Dict:
|
||||
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
|
||||
WHERE ServermodelId=$1
|
||||
'''
|
||||
|
@ -12,12 +12,15 @@ from .storage import storage_server, storage_servermodel
|
||||
from ...controller import Controller
|
||||
from ...register import register
|
||||
from ...dispatcher import dispatcher
|
||||
from ...config import get_config
|
||||
|
||||
|
||||
class Risotto(Controller):
|
||||
def __init__(self,
|
||||
test):
|
||||
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,
|
||||
type: str):
|
||||
@ -110,17 +113,15 @@ class Risotto(Controller):
|
||||
@register('v1.session.servermodel.start')
|
||||
async def start_session_servermodel(self,
|
||||
risotto_context: Context,
|
||||
servermodel_name: str,
|
||||
source_name: str,
|
||||
release_distribution: str) -> Dict:
|
||||
servermodel_name: str) -> Dict:
|
||||
""" start a new config session for a server or a servermodel
|
||||
"""
|
||||
config_module = dispatcher.get_service('config')
|
||||
servermodel = await self.call('v1.servermodel.describe',
|
||||
risotto_context,
|
||||
servermodel_name=servermodel_name,
|
||||
source_name=source_name,
|
||||
release_distribution=release_distribution)
|
||||
source_name=self.internal_source_name,
|
||||
release_distribution=self.internal_distribution_name)
|
||||
if not servermodel or servermodel['servermodel_id'] not in config_module.servermodel:
|
||||
raise Exception(_(f'cannot find servermodel with name {servermodel_name}'))
|
||||
id = servermodel['servermodel_id']
|
||||
@ -293,7 +294,7 @@ class Risotto(Controller):
|
||||
modif_config = session['config']
|
||||
await config.value.importation(await modif_config.value.exportation())
|
||||
await config.permissive.importation(await modif_config.permissive.exportation())
|
||||
storage.del_session(session_id)
|
||||
await storage.del_session(session_id)
|
||||
return self.format_session(session_id, session)
|
||||
|
||||
@register_http('v1', '/config/server/{session_id}')
|
||||
|
@ -103,8 +103,21 @@ class Storage(object):
|
||||
raise NotAllowedError()
|
||||
return session
|
||||
|
||||
def del_session(self,
|
||||
id: int):
|
||||
async def del_session(self,
|
||||
id: int):
|
||||
config = self.sessions[id]['meta']
|
||||
while True:
|
||||
try:
|
||||
children = list(await config.config.list())
|
||||
if not children:
|
||||
# it's an empty metaconfig
|
||||
break
|
||||
config = children[0]
|
||||
await config.session.reset()
|
||||
except:
|
||||
# it's a config, so no "list" method
|
||||
break
|
||||
await self.sessions[id]['meta'].session.reset()
|
||||
del self.sessions[id]
|
||||
|
||||
|
||||
|
@ -21,41 +21,45 @@ class Risotto(Controller):
|
||||
async def template_get(self,
|
||||
risotto_context,
|
||||
server_name: str) -> Dict:
|
||||
# get informations for server
|
||||
server = await self.call('v1.server.describe',
|
||||
risotto_context,
|
||||
server_name=server_name)
|
||||
server_id = server['server_id']
|
||||
servermodel_id = server['server_servermodel_id']
|
||||
# verify if server has deployed configuration
|
||||
config_module = dispatcher.get_service('config')
|
||||
server = config_module.server[server_id]
|
||||
export = await server['server'].value.exportation()
|
||||
if not export[0]:
|
||||
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)
|
||||
while True:
|
||||
try:
|
||||
children = list(await config.config.list())
|
||||
except:
|
||||
break
|
||||
if children:
|
||||
config = children[0]
|
||||
else:
|
||||
break
|
||||
configurations_dir = join(CONFIGURATION_DIR,
|
||||
str(server_id))
|
||||
if isdir(configurations_dir):
|
||||
rmtree(configurations_dir)
|
||||
mkdir(configurations_dir)
|
||||
tmp_dir = join(TMP_DIR, str(server_id))
|
||||
if isdir(tmp_dir):
|
||||
rmtree(tmp_dir)
|
||||
mkdir(tmp_dir)
|
||||
templates_dir = join(self.cache_root_path, str(servermodel_id), 'templates')
|
||||
await generate(config,
|
||||
server['funcs_file'],
|
||||
templates_dir,
|
||||
tmp_dir,
|
||||
configurations_dir)
|
||||
|
||||
# copy deployed configuration
|
||||
async with await server['server'].config.deepcopy(storage=self.storage) as config:
|
||||
meta = config
|
||||
while True:
|
||||
try:
|
||||
children = list(await config.config.list())
|
||||
except:
|
||||
break
|
||||
if children:
|
||||
config = children[0]
|
||||
else:
|
||||
break
|
||||
configurations_dir = join(CONFIGURATION_DIR,
|
||||
str(server_id))
|
||||
if isdir(configurations_dir):
|
||||
rmtree(configurations_dir)
|
||||
mkdir(configurations_dir)
|
||||
tmp_dir = join(TMP_DIR, str(server_id))
|
||||
if isdir(tmp_dir):
|
||||
rmtree(tmp_dir)
|
||||
mkdir(tmp_dir)
|
||||
templates_dir = join(self.cache_root_path, str(servermodel_id), 'templates')
|
||||
await generate(config,
|
||||
server['funcs_file'],
|
||||
templates_dir,
|
||||
tmp_dir,
|
||||
configurations_dir)
|
||||
del meta
|
||||
return {'server_name': server_name,
|
||||
'template_dir': configurations_dir}
|
||||
|
@ -12,6 +12,7 @@ class Risotto(Controller):
|
||||
for uri in ['v1.applicationservice.create',
|
||||
'v1.applicationservice.dataset.updated',
|
||||
'v1.server.create',
|
||||
'v1.servermodel.create',
|
||||
'v1.servermodel.dataset.updated',
|
||||
'v1.session.server.start',
|
||||
'v1.source.create',
|
||||
|
Loading…
x
Reference in New Issue
Block a user