From 52824302d4fc606385388978f1be51a541cfaaba Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 4 Mar 2020 07:29:12 +0100 Subject: [PATCH] add dispatcher between risotto servers --- src/risotto/config.py | 2 + src/risotto/controller.py | 19 +++++- src/risotto/dispatcher.py | 6 +- src/risotto/remote.py | 59 +++++++++++++++++++ .../applicationservice/applicationservice.py | 1 + .../services/servermodel/servermodel.py | 2 + 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 src/risotto/remote.py diff --git a/src/risotto/config.py b/src/risotto/config.py index e6de985..2543020 100644 --- a/src/risotto/config.py +++ b/src/risotto/config.py @@ -28,4 +28,6 @@ def get_config(): 'servermodel': {'internal_source': 'internal', 'internal_distribution': 'last', 'internal_release_name': 'none'}, + 'submodule': {'allow_insecure_https': False, + 'pki': '192.168.56.112'}, } diff --git a/src/risotto/controller.py b/src/risotto/controller.py index 8a8a1be..e770570 100644 --- a/src/risotto/controller.py +++ b/src/risotto/controller.py @@ -1,5 +1,7 @@ +from .config import get_config from .dispatcher import dispatcher from .context import Context +from .remote import remote class Controller: @@ -7,14 +9,20 @@ class Controller: """ def __init__(self, test: bool): - pass + self.submodule = get_config()['global']['module_name'] async def call(self, uri: str, risotto_context: Context, **kwargs): """ a wrapper to dispatcher's call""" - version, uri = uri.split('.', 1) + version, submodule, message = uri.split('.', 2) + uri = submodule + '.' + message + if submodule != self.submodule: + return await remote.call_or_publish(submodule, + version, + message, + kwargs) return await dispatcher.call(version, uri, risotto_context, @@ -25,7 +33,12 @@ class Controller: risotto_context: Context, **kwargs): """ a wrapper to dispatcher's publish""" - version, uri = uri.split('.', 1) + version, submodule, uri = uri.split('.', 2) + if submodule != self.submodule: + await remote.call_or_publish(submodule, + version, + message, + kwargs) await dispatcher.publish(version, uri, risotto_context, diff --git a/src/risotto/dispatcher.py b/src/risotto/dispatcher.py index 0abf412..a9d4ae9 100644 --- a/src/risotto/dispatcher.py +++ b/src/risotto/dispatcher.py @@ -10,6 +10,7 @@ from .logger import log from .config import get_config from .context import Context from . import register +from .remote import Remote import asyncpg @@ -179,7 +180,10 @@ class PublishDispatcher: raise err -class Dispatcher(register.RegisterDispatcher, CallDispatcher, PublishDispatcher): +class Dispatcher(register.RegisterDispatcher, + Remote, + CallDispatcher, + PublishDispatcher): """ Manage message (call or publish) so launch a function when a message is called """ diff --git a/src/risotto/remote.py b/src/risotto/remote.py new file mode 100644 index 0000000..0d56a03 --- /dev/null +++ b/src/risotto/remote.py @@ -0,0 +1,59 @@ +from aiohttp import ClientSession +from requests import get, post +from json import dumps +from tiramisu_api import Config + + +from .config import get_config +# +# +# ALLOW_INSECURE_HTTPS = get_config()['submodule']['allow_insecure_https'] + + +class Remote: + submodules = {} + + async def _get_config(self, + submodule: str, + url: str) -> None: + if submodule not in self.submodules: + session = ClientSession() + async with session.get(url) as resp: + if resp.status != 200: + try: + json = await resp.json() + err = json['error']['kwargs']['reason'] + except: + err = await resp.text() + raise Exception(err) + json = await resp.json() + self.submodules[submodule] = json + return Config(self.submodules[submodule]) + + async def call_or_publish(self, + submodule: str, + version: str, + message: str, + payload) -> dict: + domain_name = get_config()['submodule'][submodule] + remote_url = f'http://{domain_name}:8080/api/{version}' + message_url = f'{remote_url}/{message}' + + config = await self._get_config(submodule, + remote_url) + print(config) + for key, value in payload.items(): + path = message + '.' + key + config.option(path).value.set(value) + session = ClientSession() + print(message_url) + async with session.post(message_url, data=dumps(payload)) as resp: + response = await resp.json() + if 'error' in response: + if 'reason' in response['error']['kwargs']: + raise Exception("{}".format(response['error']['kwargs']['reason'])) + raise Exception('erreur inconnue') + return response['response'] + + +remote = Remote() diff --git a/src/risotto/services/applicationservice/applicationservice.py b/src/risotto/services/applicationservice/applicationservice.py index acee427..97490aa 100644 --- a/src/risotto/services/applicationservice/applicationservice.py +++ b/src/risotto/services/applicationservice/applicationservice.py @@ -18,6 +18,7 @@ class Risotto(Controller): self.internal_source_name = get_config()['servermodel']['internal_source'] self.internal_distribution_name = get_config()['servermodel']['internal_distribution'] self.internal_release_name = get_config()['servermodel']['internal_release_name'] + super().__init__(test) async def on_join(self, risotto_context: Context) -> None: diff --git a/src/risotto/services/servermodel/servermodel.py b/src/risotto/services/servermodel/servermodel.py index 84c0ea0..9876c31 100644 --- a/src/risotto/services/servermodel/servermodel.py +++ b/src/risotto/services/servermodel/servermodel.py @@ -23,9 +23,11 @@ class Risotto(Generator): self.internal_release_name = get_config()['servermodel']['internal_release_name'] if not isdir(self.cache_root_path): makedirs(join(self.cache_root_path)) + super().__init__(test) async def on_join(self, risotto_context: Context) -> None: + print('===', await self.call('v1.pki.openssh.get', risotto_context)) internal_release = await self.call('v1.setting.source.release.describe', risotto_context, source_name=self.internal_source_name,