Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/18933.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Split loading config from homeserver `setup`.
17 changes: 11 additions & 6 deletions synapse/app/admin_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
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

Expand Down Expand Up @@ -256,7 +256,7 @@ def finished(self) -> str:
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)

Expand All @@ -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"

Expand Down Expand Up @@ -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)

Expand All @@ -337,5 +341,6 @@ async def run() -> 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)
7 changes: 4 additions & 3 deletions synapse/app/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/client_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/event_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/federation_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/federation_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/frontend_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
22 changes: 18 additions & 4 deletions synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,26 @@ def start_listening(self) -> None:
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",
Expand Down Expand Up @@ -368,8 +381,9 @@ async def start() -> 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__":
Expand Down
28 changes: 24 additions & 4 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,17 +308,21 @@ def start_listening(self) -> None:
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:]`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed this to be argv_options given we expect to be passed everything after the executable name in the argv list.


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")
Expand All @@ -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 "
Expand Down Expand Up @@ -405,10 +423,12 @@ def run(hs: HomeServer) -> None:


def main() -> None:
homeserver_config = load_or_generate_config(sys.argv[1:])

with LoggingContext("main"):
Comment on lines +426 to 428
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make things more clear, with #18868, this will look like the following in the future:

Suggested change
homeserver_config = load_or_generate_config(sys.argv[1:])
with LoggingContext("main"):
homeserver_config = load_or_generate_config(sys.argv[1:])
with LoggingContext(name="main", server_name=homeserver_config.server.server_name):

# 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:
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/synchrotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
7 changes: 4 additions & 3 deletions synapse/app/user_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
8 changes: 6 additions & 2 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,16 @@ def load_config_with_parser(

@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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior art, something to do in the future

argv_options: The options passed to Synapse. Usually `sys.argv[1:]`.
Returns:
Config object, or None if --generate-config or --generate-keys was set
"""
Expand Down Expand Up @@ -747,7 +751,7 @@ def load_or_generate_config(
)

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)

Expand Down
5 changes: 4 additions & 1 deletion tests/app/test_homeserver_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ def test_wrong_start_caught(self) -> None:
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)
5 changes: 4 additions & 1 deletion tests/config/test_registration_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,7 @@ def test_refuse_to_start_if_open_registration_and_no_verification(self) -> None:

# 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)
Loading