Compare commits

...

4 Commits

Author SHA1 Message Date
Eric Eastwood
4b3d923a47 Add changelog 2025-09-17 13:52:19 -05:00
Eric Eastwood
1d731ec93d Remove server_name changes
From https://github.com/element-hq/synapse/pull/18868
2025-09-17 13:46:34 -05:00
Eric Eastwood
c076082191 Fill out other entrypoints
Changes from 3a5bab7a13

```
git checkout 3a5bab7a13 -- synapse/app/admin_cmd.py synapse/app/appservice.py synapse/app/client_reader.py synapse/app/event_creator.py synapse/app/federation_reader.py synapse/app/federation_sender.py synapse/app/frontend_proxy.py synapse/app/generic_worker.py synapse/app/media_repository.py synapse/app/pusher.py synapse/app/synchrotron.py synapse/app/user_dir.py
```
2025-09-17 13:42:04 -05:00
Eric Eastwood
37ea1ae686 Split loading config vs setting up the homeserver
This allows us to get access to `server_name` before
which we may want to use in the `with LoggingContext("main"):`
call early on.

This also allows us more flexibility to parse config however
we want and setup a Synapse homeserver. Like what we do
in Synapse Pro for Small Hosts.
2025-09-17 13:35:04 -05:00
17 changed files with 109 additions and 54 deletions

1
changelog.d/18933.misc Normal file
View File

@@ -0,0 +1 @@
Split loading config from homeserver `setup`.

View File

@@ -24,7 +24,7 @@ import logging
import os
import sys
import tempfile
from typing import List, Mapping, Optional, Sequence
from typing import List, Mapping, Optional, Sequence, Tuple
from twisted.internet import defer, task
@@ -256,7 +256,7 @@ class FileExfiltrationWriter(ExfiltrationWriter):
return self.base_directory
def start(config_options: List[str]) -> None:
def load_config(argv_options: List[str]) -> Tuple[HomeServerConfig, argparse.Namespace]:
parser = argparse.ArgumentParser(description="Synapse Admin Command")
HomeServerConfig.add_arguments_to_parser(parser)
@@ -282,11 +282,15 @@ def start(config_options: List[str]) -> None:
export_data_parser.set_defaults(func=export_data_command)
try:
config, args = HomeServerConfig.load_config_with_parser(parser, config_options)
config, args = HomeServerConfig.load_config_with_parser(parser, argv_options)
except ConfigError as e:
sys.stderr.write("\n" + str(e) + "\n")
sys.exit(1)
return config, args
def start(config: HomeServerConfig, args: argparse.Namespace) -> None:
if config.worker.worker_app is not None:
assert config.worker.worker_app == "synapse.app.admin_cmd"
@@ -325,7 +329,7 @@ def start(config_options: List[str]) -> None:
# command.
async def run() -> None:
with LoggingContext("command"):
with LoggingContext(name="command"):
await _base.start(ss)
await args.func(ss, args)
@@ -337,5 +341,6 @@ def start(config_options: List[str]) -> None:
if __name__ == "__main__":
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config, args = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config, args)

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -20,13 +20,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -310,13 +310,26 @@ class GenericWorkerServer(HomeServer):
self.get_replication_command_handler().start_replication(self)
def start(config_options: List[str]) -> None:
def load_config(argv_options: List[str]) -> HomeServerConfig:
"""
Parse the commandline and config files (does not generate config)
Args:
argv_options: The options passed to Synapse. Usually `sys.argv[1:]`.
Returns:
Config object.
"""
try:
config = HomeServerConfig.load_config("Synapse worker", config_options)
config = HomeServerConfig.load_config("Synapse worker", argv_options)
except ConfigError as e:
sys.stderr.write("\n" + str(e) + "\n")
sys.exit(1)
return config
def start(config: HomeServerConfig) -> None:
# For backwards compatibility let any of the old app names.
assert config.worker.worker_app in (
"synapse.app.appservice",
@@ -355,12 +368,7 @@ def start(config_options: List[str]) -> None:
except Exception as e:
handle_startup_exception(e)
async def start() -> None:
# Re-establish log context now that we're back from the reactor
with LoggingContext("start"):
await _base.start(hs)
register_start(start)
register_start(_base.start, hs)
# redirect stdio to the logs, if configured.
if not hs.config.logging.no_redirect_stdio:
@@ -370,8 +378,9 @@ def start(config_options: List[str]) -> None:
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -308,17 +308,21 @@ class SynapseHomeServer(HomeServer):
logger.warning("Unrecognized listener type: %s", listener.type)
def setup(config_options: List[str]) -> SynapseHomeServer:
def load_or_generate_config(argv_options: List[str]) -> HomeServerConfig:
"""
Parse the commandline and config files
Supports generation of config files, so is used for the main homeserver app.
Args:
config_options_options: The options passed to Synapse. Usually `sys.argv[1:]`.
argv_options: The options passed to Synapse. Usually `sys.argv[1:]`.
Returns:
A homeserver instance.
"""
try:
config = HomeServerConfig.load_or_generate_config(
"Synapse Homeserver", config_options
"Synapse Homeserver", argv_options
)
except ConfigError as e:
sys.stderr.write("\n")
@@ -332,6 +336,20 @@ def setup(config_options: List[str]) -> SynapseHomeServer:
# generating config files and shouldn't try to continue.
sys.exit(0)
return config
def setup(config: HomeServerConfig) -> SynapseHomeServer:
"""
Create and setup a Synapse homeserver instance given a configuration.
Args:
config: The configuration for the homeserver.
Returns:
A homeserver instance.
"""
if config.worker.worker_app:
raise ConfigError(
"You have specified `worker_app` in the config but are attempting to start a non-worker "
@@ -407,10 +425,12 @@ def run(hs: HomeServer) -> None:
def main() -> None:
homeserver_config = load_or_generate_config(sys.argv[1:])
with LoggingContext("main"):
# check base requirements
check_requirements()
hs = setup(sys.argv[1:])
hs = setup(homeserver_config)
# redirect stdio to the logs, if configured.
if not hs.config.logging.no_redirect_stdio:

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -21,13 +21,14 @@
import sys
from synapse.app.generic_worker import start
from synapse.app.generic_worker import load_config, start
from synapse.util.logcontext import LoggingContext
def main() -> None:
with LoggingContext("main"):
start(sys.argv[1:])
homeserver_config = load_config(sys.argv[1:])
with LoggingContext(name="main"):
start(homeserver_config)
if __name__ == "__main__":

View File

@@ -646,12 +646,16 @@ class RootConfig:
@classmethod
def load_or_generate_config(
cls: Type[TRootConfig], description: str, argv: List[str]
cls: Type[TRootConfig], description: str, argv_options: List[str]
) -> Optional[TRootConfig]:
"""Parse the commandline and config files
Supports generation of config files, so is used for the main homeserver app.
Args:
description: TODO
argv_options: The options passed to Synapse. Usually `sys.argv[1:]`.
Returns:
Config object, or None if --generate-config or --generate-keys was set
"""
@@ -747,7 +751,7 @@ class RootConfig:
)
cls.invoke_all_static("add_arguments", parser)
config_args = parser.parse_args(argv)
config_args = parser.parse_args(argv_options)
config_files = find_config_files(search_paths=config_args.config_path)

View File

@@ -37,4 +37,7 @@ class HomeserverAppStartTestCase(ConfigFileTestCase):
self.add_lines_to_config([" main:", " host: 127.0.0.1", " port: 1234"])
# Ensure that starting master process with worker config raises an exception
with self.assertRaises(ConfigError):
synapse.app.homeserver.setup(["-c", self.config_file])
homeserver_config = synapse.app.homeserver.load_or_generate_config(
["-c", self.config_file]
)
synapse.app.homeserver.setup(homeserver_config)

View File

@@ -112,4 +112,7 @@ class RegistrationConfigTestCase(ConfigFileTestCase):
# Test that allowing open registration without verification raises an error
with self.assertRaises(ConfigError):
synapse.app.homeserver.setup(["-c", self.config_file])
homeserver_config = synapse.app.homeserver.load_or_generate_config(
["-c", self.config_file]
)
synapse.app.homeserver.setup(homeserver_config)