25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

1041 satır
36 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2017-2018 New Vector Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import argparse
  17. import errno
  18. import logging
  19. import os
  20. import re
  21. from collections import OrderedDict
  22. from enum import Enum, auto
  23. from hashlib import sha256
  24. from textwrap import dedent
  25. from typing import (
  26. Any,
  27. ClassVar,
  28. Dict,
  29. Iterable,
  30. Iterator,
  31. List,
  32. MutableMapping,
  33. Optional,
  34. Tuple,
  35. Type,
  36. TypeVar,
  37. Union,
  38. )
  39. import attr
  40. import jinja2
  41. import pkg_resources
  42. import yaml
  43. from synapse.types import StrSequence
  44. from synapse.util.templates import _create_mxc_to_http_filter, _format_ts_filter
  45. logger = logging.getLogger(__name__)
  46. class ConfigError(Exception):
  47. """Represents a problem parsing the configuration
  48. Args:
  49. msg: A textual description of the error.
  50. path: Where appropriate, an indication of where in the configuration
  51. the problem lies.
  52. """
  53. def __init__(self, msg: str, path: Optional[StrSequence] = None):
  54. self.msg = msg
  55. self.path = path
  56. def format_config_error(e: ConfigError) -> Iterator[str]:
  57. """
  58. Formats a config error neatly
  59. The idea is to format the immediate error, plus the "causes" of those errors,
  60. hopefully in a way that makes sense to the user. For example:
  61. Error in configuration at 'oidc_config.user_mapping_provider.config.display_name_template':
  62. Failed to parse config for module 'JinjaOidcMappingProvider':
  63. invalid jinja template:
  64. unexpected end of template, expected 'end of print statement'.
  65. Args:
  66. e: the error to be formatted
  67. Returns: An iterator which yields string fragments to be formatted
  68. """
  69. yield "Error in configuration"
  70. if e.path:
  71. yield " at '%s'" % (".".join(e.path),)
  72. yield ":\n %s" % (e.msg,)
  73. parent_e = e.__cause__
  74. indent = 1
  75. while parent_e:
  76. indent += 1
  77. yield ":\n%s%s" % (" " * indent, str(parent_e))
  78. parent_e = parent_e.__cause__
  79. # We split these messages out to allow packages to override with package
  80. # specific instructions.
  81. MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS = """\
  82. Please opt in or out of reporting homeserver usage statistics, by setting
  83. the `report_stats` key in your config file to either True or False.
  84. """
  85. MISSING_REPORT_STATS_SPIEL = """\
  86. We would really appreciate it if you could help our project out by reporting
  87. homeserver usage statistics from your homeserver. Your homeserver's server name,
  88. along with very basic aggregate data (e.g. number of users) will be reported. But
  89. it helps us to track the growth of the Matrix community, and helps us to make Matrix
  90. a success, as well as to convince other networks that they should peer with us.
  91. Thank you.
  92. """
  93. MISSING_SERVER_NAME = """\
  94. Missing mandatory `server_name` config option.
  95. """
  96. CONFIG_FILE_HEADER = """\
  97. # Configuration file for Synapse.
  98. #
  99. # This is a YAML file: see [1] for a quick introduction. Note in particular
  100. # that *indentation is important*: all the elements of a list or dictionary
  101. # should have the same indentation.
  102. #
  103. # [1] https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
  104. #
  105. # For more information on how to configure Synapse, including a complete accounting of
  106. # each option, go to docs/usage/configuration/config_documentation.md or
  107. # https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html
  108. """
  109. def path_exists(file_path: str) -> bool:
  110. """Check if a file exists
  111. Unlike os.path.exists, this throws an exception if there is an error
  112. checking if the file exists (for example, if there is a perms error on
  113. the parent dir).
  114. Returns:
  115. True if the file exists; False if not.
  116. """
  117. try:
  118. os.stat(file_path)
  119. return True
  120. except OSError as e:
  121. if e.errno != errno.ENOENT:
  122. raise e
  123. return False
  124. class Config:
  125. """
  126. A configuration section, containing configuration keys and values.
  127. Attributes:
  128. section: The section title of this config object, such as
  129. "tls" or "logger". This is used to refer to it on the root
  130. logger (for example, `config.tls.some_option`). Must be
  131. defined in subclasses.
  132. """
  133. section: ClassVar[str]
  134. def __init__(self, root_config: "RootConfig" = None):
  135. self.root = root_config
  136. # Get the path to the default Synapse template directory
  137. self.default_template_dir = pkg_resources.resource_filename(
  138. "synapse", "res/templates"
  139. )
  140. @staticmethod
  141. def parse_size(value: Union[str, int]) -> int:
  142. """Interpret `value` as a number of bytes.
  143. If an integer is provided it is treated as bytes and is unchanged.
  144. String byte sizes can have a suffix of 'K', `M`, `G` or `T`,
  145. representing kibibytes, mebibytes, gibibytes and tebibytes respectively.
  146. No suffix is understood as a plain byte count.
  147. Raises:
  148. TypeError, if given something other than an integer or a string
  149. ValueError: if given a string not of the form described above.
  150. """
  151. if type(value) is int: # noqa: E721
  152. return value
  153. elif isinstance(value, str):
  154. sizes = {"K": 1024, "M": 1024 * 1024, "G": 1024**3, "T": 1024**4}
  155. size = 1
  156. suffix = value[-1]
  157. if suffix in sizes:
  158. value = value[:-1]
  159. size = sizes[suffix]
  160. return int(value) * size
  161. else:
  162. raise TypeError(f"Bad byte size {value!r}")
  163. @staticmethod
  164. def parse_duration(value: Union[str, int]) -> int:
  165. """Convert a duration as a string or integer to a number of milliseconds.
  166. If an integer is provided it is treated as milliseconds and is unchanged.
  167. String durations can have a suffix of 's', 'm', 'h', 'd', 'w', or 'y'.
  168. No suffix is treated as milliseconds.
  169. Args:
  170. value: The duration to parse.
  171. Returns:
  172. The number of milliseconds in the duration.
  173. Raises:
  174. TypeError, if given something other than an integer or a string
  175. ValueError: if given a string not of the form described above.
  176. """
  177. if type(value) is int: # noqa: E721
  178. return value
  179. elif isinstance(value, str):
  180. second = 1000
  181. minute = 60 * second
  182. hour = 60 * minute
  183. day = 24 * hour
  184. week = 7 * day
  185. year = 365 * day
  186. sizes = {
  187. "s": second,
  188. "m": minute,
  189. "h": hour,
  190. "d": day,
  191. "w": week,
  192. "y": year,
  193. }
  194. size = 1
  195. suffix = value[-1]
  196. if suffix in sizes:
  197. value = value[:-1]
  198. size = sizes[suffix]
  199. return int(value) * size
  200. else:
  201. raise TypeError(f"Bad duration {value!r}")
  202. @staticmethod
  203. def abspath(file_path: str) -> str:
  204. return os.path.abspath(file_path) if file_path else file_path
  205. @classmethod
  206. def path_exists(cls, file_path: str) -> bool:
  207. return path_exists(file_path)
  208. @classmethod
  209. def check_file(cls, file_path: Optional[str], config_name: str) -> str:
  210. if file_path is None:
  211. raise ConfigError("Missing config for %s." % (config_name,))
  212. try:
  213. os.stat(file_path)
  214. except OSError as e:
  215. raise ConfigError(
  216. "Error accessing file '%s' (config for %s): %s"
  217. % (file_path, config_name, e.strerror)
  218. )
  219. return cls.abspath(file_path)
  220. @classmethod
  221. def ensure_directory(cls, dir_path: str) -> str:
  222. dir_path = cls.abspath(dir_path)
  223. os.makedirs(dir_path, exist_ok=True)
  224. if not os.path.isdir(dir_path):
  225. raise ConfigError("%s is not a directory" % (dir_path,))
  226. return dir_path
  227. @classmethod
  228. def read_file(cls, file_path: Any, config_name: str) -> str:
  229. """Deprecated: call read_file directly"""
  230. return read_file(file_path, (config_name,))
  231. def read_template(self, filename: str) -> jinja2.Template:
  232. """Load a template file from disk.
  233. This function will attempt to load the given template from the default Synapse
  234. template directory.
  235. Files read are treated as Jinja templates. The templates is not rendered yet
  236. and has autoescape enabled.
  237. Args:
  238. filename: A template filename to read.
  239. Raises:
  240. ConfigError: if the file's path is incorrect or otherwise cannot be read.
  241. Returns:
  242. A jinja2 template.
  243. """
  244. return self.read_templates([filename])[0]
  245. def read_templates(
  246. self,
  247. filenames: List[str],
  248. custom_template_directories: Optional[Iterable[str]] = None,
  249. ) -> List[jinja2.Template]:
  250. """Load a list of template files from disk using the given variables.
  251. This function will attempt to load the given templates from the default Synapse
  252. template directory. If `custom_template_directories` is supplied, any directory
  253. in this list is tried (in the order they appear in the list) before trying
  254. Synapse's default directory.
  255. Files read are treated as Jinja templates. The templates are not rendered yet
  256. and have autoescape enabled.
  257. Args:
  258. filenames: A list of template filenames to read.
  259. custom_template_directories: A list of directory to try to look for the
  260. templates before using the default Synapse template directory instead.
  261. Raises:
  262. ConfigError: if the file's path is incorrect or otherwise cannot be read.
  263. Returns:
  264. A list of jinja2 templates.
  265. """
  266. search_directories = []
  267. # The loader will first look in the custom template directories (if specified)
  268. # for the given filename. If it doesn't find it, it will use the default
  269. # template dir instead.
  270. if custom_template_directories is not None:
  271. for custom_template_directory in custom_template_directories:
  272. # Check that the given template directory exists
  273. if not self.path_exists(custom_template_directory):
  274. raise ConfigError(
  275. "Configured template directory does not exist: %s"
  276. % (custom_template_directory,)
  277. )
  278. # Search the custom template directory as well
  279. search_directories.append(custom_template_directory)
  280. # Append the default directory at the end of the list so Jinja can fallback on it
  281. # if a template is missing from any custom directory.
  282. search_directories.append(self.default_template_dir)
  283. # TODO: switch to synapse.util.templates.build_jinja_env
  284. loader = jinja2.FileSystemLoader(search_directories)
  285. env = jinja2.Environment(
  286. loader=loader,
  287. autoescape=jinja2.select_autoescape(),
  288. )
  289. # Update the environment with our custom filters
  290. env.filters.update(
  291. {
  292. "format_ts": _format_ts_filter,
  293. "mxc_to_http": _create_mxc_to_http_filter(
  294. self.root.server.public_baseurl
  295. ),
  296. }
  297. )
  298. # Load the templates
  299. return [env.get_template(filename) for filename in filenames]
  300. TRootConfig = TypeVar("TRootConfig", bound="RootConfig")
  301. class RootConfig:
  302. """
  303. Holder of an application's configuration.
  304. What configuration this object holds is defined by `config_classes`, a list
  305. of Config classes that will be instantiated and given the contents of a
  306. configuration file to read. They can then be accessed on this class by their
  307. section name, defined in the Config or dynamically set to be the name of the
  308. class, lower-cased and with "Config" removed.
  309. """
  310. config_classes: List[Type[Config]] = []
  311. def __init__(self, config_files: StrSequence = ()):
  312. # Capture absolute paths here, so we can reload config after we daemonize.
  313. self.config_files = [os.path.abspath(path) for path in config_files]
  314. for config_class in self.config_classes:
  315. if config_class.section is None:
  316. raise ValueError("%r requires a section name" % (config_class,))
  317. try:
  318. conf = config_class(self)
  319. except Exception as e:
  320. raise Exception("Failed making %s: %r" % (config_class.section, e))
  321. setattr(self, config_class.section, conf)
  322. def invoke_all(
  323. self, func_name: str, *args: Any, **kwargs: Any
  324. ) -> MutableMapping[str, Any]:
  325. """
  326. Invoke a function on all instantiated config objects this RootConfig is
  327. configured to use.
  328. Args:
  329. func_name: Name of function to invoke
  330. *args
  331. **kwargs
  332. Returns:
  333. ordered dictionary of config section name and the result of the
  334. function from it.
  335. """
  336. res = OrderedDict()
  337. for config_class in self.config_classes:
  338. config = getattr(self, config_class.section)
  339. if hasattr(config, func_name):
  340. res[config_class.section] = getattr(config, func_name)(*args, **kwargs)
  341. return res
  342. @classmethod
  343. def invoke_all_static(cls, func_name: str, *args: Any, **kwargs: any) -> None:
  344. """
  345. Invoke a static function on config objects this RootConfig is
  346. configured to use.
  347. Args:
  348. func_name: Name of function to invoke
  349. *args
  350. **kwargs
  351. Returns:
  352. ordered dictionary of config section name and the result of the
  353. function from it.
  354. """
  355. for config in cls.config_classes:
  356. if hasattr(config, func_name):
  357. getattr(config, func_name)(*args, **kwargs)
  358. def generate_config(
  359. self,
  360. config_dir_path: str,
  361. data_dir_path: str,
  362. server_name: str,
  363. generate_secrets: bool = False,
  364. report_stats: Optional[bool] = None,
  365. open_private_ports: bool = False,
  366. listeners: Optional[List[dict]] = None,
  367. tls_certificate_path: Optional[str] = None,
  368. tls_private_key_path: Optional[str] = None,
  369. ) -> str:
  370. """
  371. Build a default configuration file
  372. This is used when the user explicitly asks us to generate a config file
  373. (eg with --generate-config).
  374. Args:
  375. config_dir_path: The path where the config files are kept. Used to
  376. create filenames for things like the log config and the signing key.
  377. data_dir_path: The path where the data files are kept. Used to create
  378. filenames for things like the database and media store.
  379. server_name: The server name. Used to initialise the server_name
  380. config param, but also used in the names of some of the config files.
  381. generate_secrets: True if we should generate new secrets for things
  382. like the macaroon_secret_key. If False, these parameters will be left
  383. unset.
  384. report_stats: Initial setting for the report_stats setting.
  385. If None, report_stats will be left unset.
  386. open_private_ports: True to leave private ports (such as the non-TLS
  387. HTTP listener) open to the internet.
  388. listeners: A list of descriptions of the listeners synapse should
  389. start with each of which specifies a port (int), a list of
  390. resources (list(str)), tls (bool) and type (str). For example:
  391. [{
  392. "port": 8448,
  393. "resources": [{"names": ["federation"]}],
  394. "tls": True,
  395. "type": "http",
  396. },
  397. {
  398. "port": 443,
  399. "resources": [{"names": ["client"]}],
  400. "tls": False,
  401. "type": "http",
  402. }],
  403. tls_certificate_path: The path to the tls certificate.
  404. tls_private_key_path: The path to the tls private key.
  405. Returns:
  406. The yaml config file
  407. """
  408. conf = CONFIG_FILE_HEADER + "\n".join(
  409. dedent(conf)
  410. for conf in self.invoke_all(
  411. "generate_config_section",
  412. config_dir_path=config_dir_path,
  413. data_dir_path=data_dir_path,
  414. server_name=server_name,
  415. generate_secrets=generate_secrets,
  416. report_stats=report_stats,
  417. open_private_ports=open_private_ports,
  418. listeners=listeners,
  419. tls_certificate_path=tls_certificate_path,
  420. tls_private_key_path=tls_private_key_path,
  421. ).values()
  422. )
  423. conf = re.sub("\n{2,}", "\n", conf)
  424. return conf
  425. @classmethod
  426. def load_config(
  427. cls: Type[TRootConfig], description: str, argv: List[str]
  428. ) -> TRootConfig:
  429. """Parse the commandline and config files
  430. Doesn't support config-file-generation: used by the worker apps.
  431. Returns:
  432. Config object.
  433. """
  434. config_parser = argparse.ArgumentParser(description=description)
  435. cls.add_arguments_to_parser(config_parser)
  436. obj, _ = cls.load_config_with_parser(config_parser, argv)
  437. return obj
  438. @classmethod
  439. def add_arguments_to_parser(cls, config_parser: argparse.ArgumentParser) -> None:
  440. """Adds all the config flags to an ArgumentParser.
  441. Doesn't support config-file-generation: used by the worker apps.
  442. Used for workers where we want to add extra flags/subcommands.
  443. Args:
  444. config_parser: App description
  445. """
  446. config_parser.add_argument(
  447. "-c",
  448. "--config-path",
  449. action="append",
  450. metavar="CONFIG_FILE",
  451. help="Specify config file. Can be given multiple times and"
  452. " may specify directories containing *.yaml files.",
  453. )
  454. config_parser.add_argument(
  455. "--keys-directory",
  456. metavar="DIRECTORY",
  457. help="Where files such as certs and signing keys are stored when"
  458. " their location is not given explicitly in the config."
  459. " Defaults to the directory containing the last config file",
  460. )
  461. cls.invoke_all_static("add_arguments", config_parser)
  462. @classmethod
  463. def load_config_with_parser(
  464. cls: Type[TRootConfig], parser: argparse.ArgumentParser, argv: List[str]
  465. ) -> Tuple[TRootConfig, argparse.Namespace]:
  466. """Parse the commandline and config files with the given parser
  467. Doesn't support config-file-generation: used by the worker apps.
  468. Used for workers where we want to add extra flags/subcommands.
  469. Args:
  470. parser
  471. argv
  472. Returns:
  473. Returns the parsed config object and the parsed argparse.Namespace
  474. object from parser.parse_args(..)`
  475. """
  476. config_args = parser.parse_args(argv)
  477. config_files = find_config_files(search_paths=config_args.config_path)
  478. obj = cls(config_files)
  479. if not config_files:
  480. parser.error("Must supply a config file.")
  481. if config_args.keys_directory:
  482. config_dir_path = config_args.keys_directory
  483. else:
  484. config_dir_path = os.path.dirname(config_files[-1])
  485. config_dir_path = os.path.abspath(config_dir_path)
  486. data_dir_path = os.getcwd()
  487. config_dict = read_config_files(config_files)
  488. obj.parse_config_dict(
  489. config_dict, config_dir_path=config_dir_path, data_dir_path=data_dir_path
  490. )
  491. obj.invoke_all("read_arguments", config_args)
  492. return obj, config_args
  493. @classmethod
  494. def load_or_generate_config(
  495. cls: Type[TRootConfig], description: str, argv: List[str]
  496. ) -> Optional[TRootConfig]:
  497. """Parse the commandline and config files
  498. Supports generation of config files, so is used for the main homeserver app.
  499. Returns:
  500. Config object, or None if --generate-config or --generate-keys was set
  501. """
  502. parser = argparse.ArgumentParser(description=description)
  503. parser.add_argument(
  504. "-c",
  505. "--config-path",
  506. action="append",
  507. metavar="CONFIG_FILE",
  508. help="Specify config file. Can be given multiple times and"
  509. " may specify directories containing *.yaml files.",
  510. )
  511. # we nest the mutually-exclusive group inside another group so that the help
  512. # text shows them in their own group.
  513. generate_mode_group = parser.add_argument_group(
  514. "Config generation mode",
  515. )
  516. generate_mode_exclusive = generate_mode_group.add_mutually_exclusive_group()
  517. generate_mode_exclusive.add_argument(
  518. # hidden option to make the type and default work
  519. "--generate-mode",
  520. help=argparse.SUPPRESS,
  521. type=_ConfigGenerateMode,
  522. default=_ConfigGenerateMode.GENERATE_MISSING_AND_RUN,
  523. )
  524. generate_mode_exclusive.add_argument(
  525. "--generate-config",
  526. help="Generate a config file, then exit.",
  527. action="store_const",
  528. const=_ConfigGenerateMode.GENERATE_EVERYTHING_AND_EXIT,
  529. dest="generate_mode",
  530. )
  531. generate_mode_exclusive.add_argument(
  532. "--generate-missing-configs",
  533. "--generate-keys",
  534. help="Generate any missing additional config files, then exit.",
  535. action="store_const",
  536. const=_ConfigGenerateMode.GENERATE_MISSING_AND_EXIT,
  537. dest="generate_mode",
  538. )
  539. generate_mode_exclusive.add_argument(
  540. "--generate-missing-and-run",
  541. help="Generate any missing additional config files, then run. This is the "
  542. "default behaviour.",
  543. action="store_const",
  544. const=_ConfigGenerateMode.GENERATE_MISSING_AND_RUN,
  545. dest="generate_mode",
  546. )
  547. generate_group = parser.add_argument_group("Details for --generate-config")
  548. generate_group.add_argument(
  549. "-H", "--server-name", help="The server name to generate a config file for."
  550. )
  551. generate_group.add_argument(
  552. "--report-stats",
  553. action="store",
  554. help="Whether the generated config reports homeserver usage statistics.",
  555. choices=["yes", "no"],
  556. )
  557. generate_group.add_argument(
  558. "--config-directory",
  559. "--keys-directory",
  560. metavar="DIRECTORY",
  561. help=(
  562. "Specify where additional config files such as signing keys and log"
  563. " config should be stored. Defaults to the same directory as the last"
  564. " config file."
  565. ),
  566. )
  567. generate_group.add_argument(
  568. "--data-directory",
  569. metavar="DIRECTORY",
  570. help=(
  571. "Specify where data such as the media store and database file should be"
  572. " stored. Defaults to the current working directory."
  573. ),
  574. )
  575. generate_group.add_argument(
  576. "--open-private-ports",
  577. action="store_true",
  578. help=(
  579. "Leave private ports (such as the non-TLS HTTP listener) open to the"
  580. " internet. Do not use this unless you know what you are doing."
  581. ),
  582. )
  583. cls.invoke_all_static("add_arguments", parser)
  584. config_args = parser.parse_args(argv)
  585. config_files = find_config_files(search_paths=config_args.config_path)
  586. if not config_files:
  587. parser.error(
  588. "Must supply a config file.\nA config file can be automatically"
  589. ' generated using "--generate-config -H SERVER_NAME'
  590. ' -c CONFIG-FILE"'
  591. )
  592. if config_args.config_directory:
  593. config_dir_path = config_args.config_directory
  594. else:
  595. config_dir_path = os.path.dirname(config_files[-1])
  596. config_dir_path = os.path.abspath(config_dir_path)
  597. data_dir_path = os.getcwd()
  598. obj = cls(config_files)
  599. if (
  600. config_args.generate_mode
  601. == _ConfigGenerateMode.GENERATE_EVERYTHING_AND_EXIT
  602. ):
  603. if config_args.report_stats is None:
  604. parser.error(
  605. "Please specify either --report-stats=yes or --report-stats=no\n\n"
  606. + MISSING_REPORT_STATS_SPIEL
  607. )
  608. (config_path,) = config_files
  609. if not path_exists(config_path):
  610. print("Generating config file %s" % (config_path,))
  611. if config_args.data_directory:
  612. data_dir_path = config_args.data_directory
  613. else:
  614. data_dir_path = os.getcwd()
  615. data_dir_path = os.path.abspath(data_dir_path)
  616. server_name = config_args.server_name
  617. if not server_name:
  618. raise ConfigError(
  619. "Must specify a server_name to a generate config for."
  620. " Pass -H server.name."
  621. )
  622. config_str = obj.generate_config(
  623. config_dir_path=config_dir_path,
  624. data_dir_path=data_dir_path,
  625. server_name=server_name,
  626. report_stats=(config_args.report_stats == "yes"),
  627. generate_secrets=True,
  628. open_private_ports=config_args.open_private_ports,
  629. )
  630. os.makedirs(config_dir_path, exist_ok=True)
  631. with open(config_path, "w") as config_file:
  632. config_file.write(config_str)
  633. config_file.write("\n\n# vim:ft=yaml")
  634. config_dict = yaml.safe_load(config_str)
  635. obj.generate_missing_files(config_dict, config_dir_path)
  636. print(
  637. (
  638. "A config file has been generated in %r for server name"
  639. " %r. Please review this file and customise it"
  640. " to your needs."
  641. )
  642. % (config_path, server_name)
  643. )
  644. return
  645. else:
  646. print(
  647. (
  648. "Config file %r already exists. Generating any missing config"
  649. " files."
  650. )
  651. % (config_path,)
  652. )
  653. config_dict = read_config_files(config_files)
  654. obj.generate_missing_files(config_dict, config_dir_path)
  655. if config_args.generate_mode in (
  656. _ConfigGenerateMode.GENERATE_EVERYTHING_AND_EXIT,
  657. _ConfigGenerateMode.GENERATE_MISSING_AND_EXIT,
  658. ):
  659. return None
  660. obj.parse_config_dict(
  661. config_dict, config_dir_path=config_dir_path, data_dir_path=data_dir_path
  662. )
  663. obj.invoke_all("read_arguments", config_args)
  664. return obj
  665. def parse_config_dict(
  666. self, config_dict: Dict[str, Any], config_dir_path: str, data_dir_path: str
  667. ) -> None:
  668. """Read the information from the config dict into this Config object.
  669. Args:
  670. config_dict: Configuration data, as read from the yaml
  671. config_dir_path: The path where the config files are kept. Used to
  672. create filenames for things like the log config and the signing key.
  673. data_dir_path: The path where the data files are kept. Used to create
  674. filenames for things like the database and media store.
  675. """
  676. self.invoke_all(
  677. "read_config",
  678. config_dict,
  679. config_dir_path=config_dir_path,
  680. data_dir_path=data_dir_path,
  681. )
  682. def generate_missing_files(
  683. self, config_dict: Dict[str, Any], config_dir_path: str
  684. ) -> None:
  685. self.invoke_all("generate_files", config_dict, config_dir_path)
  686. def reload_config_section(self, section_name: str) -> Config:
  687. """Reconstruct the given config section, leaving all others unchanged.
  688. This works in three steps:
  689. 1. Create a new instance of the relevant `Config` subclass.
  690. 2. Call `read_config` on that instance to parse the new config.
  691. 3. Replace the existing config instance with the new one.
  692. :raises ValueError: if the given `section` does not exist.
  693. :raises ConfigError: for any other problems reloading config.
  694. :returns: the previous config object, which no longer has a reference to this
  695. RootConfig.
  696. """
  697. existing_config: Optional[Config] = getattr(self, section_name, None)
  698. if existing_config is None:
  699. raise ValueError(f"Unknown config section '{section_name}'")
  700. logger.info("Reloading config section '%s'", section_name)
  701. new_config_data = read_config_files(self.config_files)
  702. new_config = type(existing_config)(self)
  703. new_config.read_config(new_config_data)
  704. setattr(self, section_name, new_config)
  705. existing_config.root = None
  706. return existing_config
  707. def read_config_files(config_files: Iterable[str]) -> Dict[str, Any]:
  708. """Read the config files into a dict
  709. Args:
  710. config_files: A list of the config files to read
  711. Returns:
  712. The configuration dictionary.
  713. """
  714. specified_config = {}
  715. for config_file in config_files:
  716. with open(config_file) as file_stream:
  717. yaml_config = yaml.safe_load(file_stream)
  718. if not isinstance(yaml_config, dict):
  719. err = "File %r is empty or doesn't parse into a key-value map. IGNORING."
  720. print(err % (config_file,))
  721. continue
  722. specified_config.update(yaml_config)
  723. if "server_name" not in specified_config:
  724. raise ConfigError(MISSING_SERVER_NAME)
  725. if "report_stats" not in specified_config:
  726. raise ConfigError(
  727. MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS + "\n" + MISSING_REPORT_STATS_SPIEL
  728. )
  729. return specified_config
  730. def find_config_files(search_paths: List[str]) -> List[str]:
  731. """Finds config files using a list of search paths. If a path is a file
  732. then that file path is added to the list. If a search path is a directory
  733. then all the "*.yaml" files in that directory are added to the list in
  734. sorted order.
  735. Args:
  736. search_paths: A list of paths to search.
  737. Returns:
  738. A list of file paths.
  739. """
  740. config_files = []
  741. if search_paths:
  742. for config_path in search_paths:
  743. if os.path.isdir(config_path):
  744. # We accept specifying directories as config paths, we search
  745. # inside that directory for all files matching *.yaml, and then
  746. # we apply them in *sorted* order.
  747. files = []
  748. for entry in os.listdir(config_path):
  749. entry_path = os.path.join(config_path, entry)
  750. if not os.path.isfile(entry_path):
  751. err = "Found subdirectory in config directory: %r. IGNORING."
  752. print(err % (entry_path,))
  753. continue
  754. if not entry.endswith(".yaml"):
  755. err = (
  756. "Found file in config directory that does not end in "
  757. "'.yaml': %r. IGNORING."
  758. )
  759. print(err % (entry_path,))
  760. continue
  761. files.append(entry_path)
  762. config_files.extend(sorted(files))
  763. else:
  764. config_files.append(config_path)
  765. return config_files
  766. @attr.s(auto_attribs=True)
  767. class ShardedWorkerHandlingConfig:
  768. """Algorithm for choosing which instance is responsible for handling some
  769. sharded work.
  770. For example, the federation senders use this to determine which instances
  771. handles sending stuff to a given destination (which is used as the `key`
  772. below).
  773. """
  774. instances: List[str]
  775. def should_handle(self, instance_name: str, key: str) -> bool:
  776. """Whether this instance is responsible for handling the given key."""
  777. # If no instances are defined we assume some other worker is handling
  778. # this.
  779. if not self.instances:
  780. return False
  781. return self._get_instance(key) == instance_name
  782. def _get_instance(self, key: str) -> str:
  783. """Get the instance responsible for handling the given key.
  784. Note: For federation sending and pushers the config for which instance
  785. is sending is known only to the sender instance, so we don't expose this
  786. method by default.
  787. """
  788. if not self.instances:
  789. raise Exception("Unknown worker")
  790. if len(self.instances) == 1:
  791. return self.instances[0]
  792. # We shard by taking the hash, modulo it by the number of instances and
  793. # then checking whether this instance matches the instance at that
  794. # index.
  795. #
  796. # (Technically this introduces some bias and is not entirely uniform,
  797. # but since the hash is so large the bias is ridiculously small).
  798. dest_hash = sha256(key.encode("utf8")).digest()
  799. dest_int = int.from_bytes(dest_hash, byteorder="little")
  800. remainder = dest_int % (len(self.instances))
  801. return self.instances[remainder]
  802. @attr.s
  803. class RoutableShardedWorkerHandlingConfig(ShardedWorkerHandlingConfig):
  804. """A version of `ShardedWorkerHandlingConfig` that is used for config
  805. options where all instances know which instances are responsible for the
  806. sharded work.
  807. """
  808. def __attrs_post_init__(self):
  809. # We require that `self.instances` is non-empty.
  810. if not self.instances:
  811. raise Exception("Got empty list of instances for shard config")
  812. def get_instance(self, key: str) -> str:
  813. """Get the instance responsible for handling the given key."""
  814. return self._get_instance(key)
  815. def read_file(file_path: Any, config_path: Iterable[str]) -> str:
  816. """Check the given file exists, and read it into a string
  817. If it does not, emit an error indicating the problem
  818. Args:
  819. file_path: the file to be read
  820. config_path: where in the configuration file_path came from, so that a useful
  821. error can be emitted if it does not exist.
  822. Returns:
  823. content of the file.
  824. Raises:
  825. ConfigError if there is a problem reading the file.
  826. """
  827. if not isinstance(file_path, str):
  828. raise ConfigError("%r is not a string", config_path)
  829. try:
  830. os.stat(file_path)
  831. with open(file_path) as file_stream:
  832. return file_stream.read()
  833. except OSError as e:
  834. raise ConfigError("Error accessing file %r" % (file_path,), config_path) from e
  835. class _ConfigGenerateMode(Enum):
  836. GENERATE_MISSING_AND_RUN = auto()
  837. GENERATE_MISSING_AND_EXIT = auto()
  838. GENERATE_EVERYTHING_AND_EXIT = auto()
  839. __all__ = [
  840. "Config",
  841. "RootConfig",
  842. "ShardedWorkerHandlingConfig",
  843. "RoutableShardedWorkerHandlingConfig",
  844. "read_file",
  845. ]