1
0

start.py 8.8 KB

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