Skip to content

Commit

Permalink
federation activitypub bovine: Remove subprocess call to create mecha…
Browse files Browse the repository at this point in the history
…nical_bull config.toml replace with Python

Asciinema: https://asciinema.org/a/619871
Signed-off-by: John Andersen <[email protected]>
  • Loading branch information
pdxjohnny committed Nov 8, 2023
1 parent b7436f1 commit 74b9a71
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions scitt_emulator/federation_activitypub_bovine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import json
import enum
import types
import pprint
import atexit
import base64
import socket
import inspect
import tomllib
import logging
import asyncio
import pathlib
Expand All @@ -20,20 +22,16 @@
from pathlib import Path
from typing import Optional

import tomli
import tomli_w
import bovine
import aiohttp
from bovine_store import BovineAdminStore
from bovine_herd import BovineHerd
from bovine_pubsub import BovinePubSub
from bovine.activitystreams import factories_for_actor_object
from bovine.clients import lookup_uri_with_webfinger
from bovine.crypto import generate_ed25519_private_key, private_key_to_did_key
from mechanical_bull.handlers import (
HandlerEvent,
HandlerAPIVersion,
load_handlers,
build_handler,
)

from scitt_emulator.scitt import SCITTServiceEmulator
Expand All @@ -43,8 +41,6 @@

logger = logging.getLogger(__name__)

import pprint


class SCITTFederationActivityPubBovine(SCITTFederation):
def __init__(self, app, signals, config_path):
Expand Down Expand Up @@ -86,32 +82,31 @@ async def initialize_service(self):
config_toml_path = pathlib.Path(self.workspace, "config.toml")
if not config_toml_path.exists():
logger.info("Actor client config does not exist, creating...")
cmd = [
sys.executable,
"-um",
"mechanical_bull.add_user",
"--accept",
self.handle_name,
self.domain,
]
subprocess.check_call(
cmd,
cwd=self.workspace,
)
logger.info("Actor client config created")
private_key = generate_ed25519_private_key()
did_key = private_key_to_did_key(private_key)
config_toml_obj = {
self.handle_name: {
"secret": private_key,
"host": self.domain,
"domain": self.domain,
"handlers": {
"mechanical_bull.actions.accept_follow_request": True,
},
},
}
config_toml_path.write_text(tomli_w.dumps(config_toml_obj))
logger.info("Actor client config.toml created")
else:
config_toml_obj = tomllib.loads(config_toml_path.read_text())
did_key = config_toml_obj[self.handle_name]["secret"]

config_toml_obj = tomli.loads(config_toml_path.read_text())
# Enable handler() function in this file for this actor
config_toml_obj[self.handle_name]["handlers"][
inspect.getmodule(sys.modules[__name__]).__spec__.name
] = {
"signals": self.signals,
"following": self.config.get("following", {}),
}
# Extract public key from private key in config file
did_key = bovine.crypto.private_key_to_did_key(
config_toml_obj[self.handle_name]["secret"],
)

bovine_store = self.app.config["bovine_store"]
_account, actor_url = await bovine_store.get_account_url_for_identity(did_key)
Expand All @@ -134,24 +129,15 @@ async def initialize_service(self):
].get_account_url_for_identity(did_key)
logger.info("Actor key added in database. actor_url is %s", actor_url)

# Run client handlers
async def mechanical_bull_loop(config):
try:
for client_name, client_config in config.items():
if isinstance(client_config, dict):
handlers = load_handlers(client_config["handlers"])
client_config["domain"] = client_config["host"]
self.app.add_background_task(
loop, client_name, client_config, handlers
)
except Exception as e:
logger.exception(e)

# async with aiohttp.ClientSession(trust_env=True) as client_session:
async with contextlib.AsyncExitStack() as async_exit_stack:
# await mechanical_bull_loop(config_toml_obj)
self.app.config["bovine_async_exit_stack"] = async_exit_stack
self.app.add_background_task(mechanical_bull_loop, config_toml_obj)
self.app.add_background_task(
mechanical_bull_loop,
config_toml_obj,
add_background_task=self.app.add_background_task,
)
yield


Expand All @@ -167,6 +153,7 @@ class HandlerEvent(enum.Enum):
OPENED = enum.auto()
CLOSED = enum.auto()


async def handle(
client: bovine.BovineClient,
data: dict,
Expand Down Expand Up @@ -472,3 +459,14 @@ async def loop(client_name, client_config, handlers):
logger.exception(e)
await asyncio.sleep(2**i)
i += 1


# Run client handlers using call_compat
async def mechanical_bull_loop(config, *, add_background_task=asyncio.create_task):
try:
for client_name, client_config in config.items():
if isinstance(client_config, dict):
handlers = load_handlers(client_config["handlers"])
add_background_task(loop, client_name, client_config, handlers)
except Exception as e:
logger.exception(e)

0 comments on commit 74b9a71

Please sign in to comment.