_base.py 31 KB

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