1
0

start.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/usr/local/bin/python
  2. import codecs
  3. import glob
  4. import os
  5. import platform
  6. import subprocess
  7. import sys
  8. from typing import Any, Dict, List, Mapping, MutableMapping, NoReturn, Optional
  9. import jinja2
  10. # Utility functions
  11. def log(txt: str) -> None:
  12. print(txt, file=sys.stderr)
  13. def error(txt: str) -> NoReturn:
  14. log(txt)
  15. sys.exit(2)
  16. def convert(src: str, dst: str, environ: Mapping[str, object]) -> None:
  17. """Generate a file from a template
  18. Args:
  19. src: path to input file
  20. dst: path to file to write
  21. environ: environment dictionary, for replacement mappings.
  22. """
  23. with open(src) as infile:
  24. template = infile.read()
  25. rendered = jinja2.Template(template).render(**environ)
  26. with open(dst, "w") as outfile:
  27. outfile.write(rendered)
  28. def generate_config_from_template(
  29. config_dir: str,
  30. config_path: str,
  31. os_environ: Mapping[str, str],
  32. ownership: Optional[str],
  33. ) -> None:
  34. """Generate a homeserver.yaml from environment variables
  35. Args:
  36. config_dir: where to put generated config files
  37. config_path: where to put the main config file
  38. os_environ: environment mapping
  39. ownership: "<user>:<group>" string which will be used to set
  40. ownership of the generated configs. If None, ownership will not change.
  41. """
  42. for v in ("SYNAPSE_SERVER_NAME", "SYNAPSE_REPORT_STATS"):
  43. if v not in os_environ:
  44. error(
  45. "Environment variable '%s' is mandatory when generating a config file."
  46. % (v,)
  47. )
  48. # populate some params from data files (if they exist, else create new ones)
  49. environ: Dict[str, Any] = dict(os_environ)
  50. secrets = {
  51. "registration": "SYNAPSE_REGISTRATION_SHARED_SECRET",
  52. "macaroon": "SYNAPSE_MACAROON_SECRET_KEY",
  53. }
  54. for name, secret in secrets.items():
  55. if secret not in environ:
  56. filename = "/data/%s.%s.key" % (environ["SYNAPSE_SERVER_NAME"], name)
  57. # if the file already exists, load in the existing value; otherwise,
  58. # generate a new secret and write it to a file
  59. if os.path.exists(filename):
  60. log("Reading %s from %s" % (secret, filename))
  61. with open(filename) as handle:
  62. value = handle.read()
  63. else:
  64. log("Generating a random secret for {}".format(secret))
  65. value = codecs.encode(os.urandom(32), "hex").decode()
  66. with open(filename, "w") as handle:
  67. handle.write(value)
  68. environ[secret] = value
  69. environ["SYNAPSE_APPSERVICES"] = glob.glob("/data/appservices/*.yaml")
  70. if not os.path.exists(config_dir):
  71. os.mkdir(config_dir)
  72. # Convert SYNAPSE_NO_TLS to boolean if exists
  73. if "SYNAPSE_NO_TLS" in environ:
  74. tlsanswerstring = str.lower(environ["SYNAPSE_NO_TLS"])
  75. if tlsanswerstring in ("true", "on", "1", "yes"):
  76. environ["SYNAPSE_NO_TLS"] = True
  77. else:
  78. if tlsanswerstring in ("false", "off", "0", "no"):
  79. environ["SYNAPSE_NO_TLS"] = False
  80. else:
  81. error(
  82. 'Environment variable "SYNAPSE_NO_TLS" found but value "'
  83. + tlsanswerstring
  84. + '" unrecognized; exiting.'
  85. )
  86. if "SYNAPSE_LOG_CONFIG" not in environ:
  87. environ["SYNAPSE_LOG_CONFIG"] = config_dir + "/log.config"
  88. log("Generating synapse config file " + config_path)
  89. convert("/conf/homeserver.yaml", config_path, environ)
  90. log_config_file = environ["SYNAPSE_LOG_CONFIG"]
  91. log("Generating log config file " + log_config_file)
  92. convert("/conf/log.config", log_config_file, environ)
  93. # Hopefully we already have a signing key, but generate one if not.
  94. args = [
  95. sys.executable,
  96. "-m",
  97. "synapse.app.homeserver",
  98. "--config-path",
  99. config_path,
  100. # tell synapse to put generated keys in /data rather than /compiled
  101. "--keys-directory",
  102. config_dir,
  103. "--generate-keys",
  104. ]
  105. if ownership is not None:
  106. log(f"Setting ownership on /data to {ownership}")
  107. subprocess.check_output(["chown", "-R", ownership, "/data"])
  108. args = ["gosu", ownership] + args
  109. subprocess.check_output(args)
  110. def run_generate_config(environ: Mapping[str, str], ownership: Optional[str]) -> None:
  111. """Run synapse with a --generate-config param to generate a template config file
  112. Args:
  113. environ: env vars from `os.enrivon`.
  114. ownership: "userid:groupid" arg for chmod. If None, ownership will not change.
  115. Never returns.
  116. """
  117. for v in ("SYNAPSE_SERVER_NAME", "SYNAPSE_REPORT_STATS"):
  118. if v not in environ:
  119. error("Environment variable '%s' is mandatory in `generate` mode." % (v,))
  120. server_name = environ["SYNAPSE_SERVER_NAME"]
  121. config_dir = environ.get("SYNAPSE_CONFIG_DIR", "/data")
  122. config_path = environ.get("SYNAPSE_CONFIG_PATH", config_dir + "/homeserver.yaml")
  123. data_dir = environ.get("SYNAPSE_DATA_DIR", "/data")
  124. if ownership is not None:
  125. # make sure that synapse has perms to write to the data dir.
  126. log(f"Setting ownership on {data_dir} to {ownership}")
  127. subprocess.check_output(["chown", ownership, data_dir])
  128. # create a suitable log config from our template
  129. log_config_file = "%s/%s.log.config" % (config_dir, server_name)
  130. if not os.path.exists(log_config_file):
  131. log("Creating log config %s" % (log_config_file,))
  132. convert("/conf/log.config", log_config_file, environ)
  133. # generate the main config file, and a signing key.
  134. args = [
  135. sys.executable,
  136. "-m",
  137. "synapse.app.homeserver",
  138. "--server-name",
  139. server_name,
  140. "--report-stats",
  141. environ["SYNAPSE_REPORT_STATS"],
  142. "--config-path",
  143. config_path,
  144. "--config-directory",
  145. config_dir,
  146. "--data-directory",
  147. data_dir,
  148. "--generate-config",
  149. "--open-private-ports",
  150. ]
  151. # log("running %s" % (args, ))
  152. os.execv(sys.executable, args)
  153. def main(args: List[str], environ: MutableMapping[str, str]) -> None:
  154. mode = args[1] if len(args) > 1 else "run"
  155. # if we were given an explicit user to switch to, do so
  156. ownership = None
  157. if "UID" in environ:
  158. desired_uid = int(environ["UID"])
  159. desired_gid = int(environ.get("GID", "991"))
  160. ownership = f"{desired_uid}:{desired_gid}"
  161. elif os.getuid() == 0:
  162. # otherwise, if we are running as root, use user 991
  163. ownership = "991:991"
  164. synapse_worker = environ.get("SYNAPSE_WORKER", "synapse.app.homeserver")
  165. # In generate mode, generate a configuration and missing keys, then exit
  166. if mode == "generate":
  167. return run_generate_config(environ, ownership)
  168. if mode == "migrate_config":
  169. # generate a config based on environment vars.
  170. config_dir = environ.get("SYNAPSE_CONFIG_DIR", "/data")
  171. config_path = environ.get(
  172. "SYNAPSE_CONFIG_PATH", config_dir + "/homeserver.yaml"
  173. )
  174. return generate_config_from_template(
  175. config_dir, config_path, environ, ownership
  176. )
  177. if mode != "run":
  178. error("Unknown execution mode '%s'" % (mode,))
  179. args = args[2:]
  180. if "-m" not in args:
  181. args = ["-m", synapse_worker] + args
  182. jemallocpath = "/usr/lib/%s-linux-gnu/libjemalloc.so.2" % (platform.machine(),)
  183. if os.path.isfile(jemallocpath):
  184. environ["LD_PRELOAD"] = jemallocpath
  185. else:
  186. log("Could not find %s, will not use" % (jemallocpath,))
  187. # if there are no config files passed to synapse, try adding the default file
  188. if not any(p.startswith("--config-path") or p.startswith("-c") for p in args):
  189. config_dir = environ.get("SYNAPSE_CONFIG_DIR", "/data")
  190. config_path = environ.get(
  191. "SYNAPSE_CONFIG_PATH", config_dir + "/homeserver.yaml"
  192. )
  193. if not os.path.exists(config_path):
  194. if "SYNAPSE_SERVER_NAME" in environ:
  195. error(
  196. """\
  197. Config file '%s' does not exist.
  198. The synapse docker image no longer supports generating a config file on-the-fly
  199. based on environment variables. You can migrate to a static config file by
  200. running with 'migrate_config'. See the README for more details.
  201. """
  202. % (config_path,)
  203. )
  204. error(
  205. "Config file '%s' does not exist. You should either create a new "
  206. "config file by running with the `generate` argument (and then edit "
  207. "the resulting file before restarting) or specify the path to an "
  208. "existing config file with the SYNAPSE_CONFIG_PATH variable."
  209. % (config_path,)
  210. )
  211. args += ["--config-path", config_path]
  212. log("Starting synapse with args " + " ".join(args))
  213. args = [sys.executable] + args
  214. if ownership is not None:
  215. args = ["gosu", ownership] + args
  216. os.execve("/usr/sbin/gosu", args, environ)
  217. else:
  218. os.execve(sys.executable, args, environ)
  219. if __name__ == "__main__":
  220. main(sys.argv, os.environ)