add dispatcher between risotto servers
This commit is contained in:
parent
2655e03172
commit
52824302d4
@ -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'},
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
"""
|
||||
|
59
src/risotto/remote.py
Normal file
59
src/risotto/remote.py
Normal file
@ -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()
|
@ -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:
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user