start.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/local/bin/python
  2. import jinja2
  3. import os
  4. import sys
  5. import subprocess
  6. import glob
  7. import codecs
  8. # Utility functions
  9. convert = lambda src, dst, environ: open(dst, "w").write(jinja2.Template(open(src).read()).render(**environ))
  10. def check_arguments(environ, args):
  11. for argument in args:
  12. if argument not in environ:
  13. print("Environment variable %s is mandatory, exiting." % argument)
  14. sys.exit(2)
  15. def generate_secrets(environ, secrets):
  16. for name, secret in secrets.items():
  17. if secret not in environ:
  18. filename = "/data/%s.%s.key" % (environ["SYNAPSE_SERVER_NAME"], name)
  19. if os.path.exists(filename):
  20. with open(filename) as handle: value = handle.read()
  21. else:
  22. print("Generating a random secret for {}".format(name))
  23. value = codecs.encode(os.urandom(32), "hex").decode()
  24. with open(filename, "w") as handle: handle.write(value)
  25. environ[secret] = value
  26. # Prepare the configuration
  27. mode = sys.argv[1] if len(sys.argv) > 1 else None
  28. environ = os.environ.copy()
  29. ownership = "{}:{}".format(environ.get("UID", 991), environ.get("GID", 991))
  30. args = ["python", "-m", "synapse.app.homeserver"]
  31. # In generate mode, generate a configuration, missing keys, then exit
  32. if mode == "generate":
  33. check_arguments(environ, ("SYNAPSE_SERVER_NAME", "SYNAPSE_REPORT_STATS", "SYNAPSE_CONFIG_PATH"))
  34. args += [
  35. "--server-name", environ["SYNAPSE_SERVER_NAME"],
  36. "--report-stats", environ["SYNAPSE_REPORT_STATS"],
  37. "--config-path", environ["SYNAPSE_CONFIG_PATH"],
  38. "--generate-config"
  39. ]
  40. os.execv("/usr/local/bin/python", args)
  41. # In normal mode, generate missing keys if any, then run synapse
  42. else:
  43. if "SYNAPSE_CONFIG_PATH" in environ:
  44. config_path = environ["SYNAPSE_CONFIG_PATH"]
  45. else:
  46. check_arguments(environ, ("SYNAPSE_SERVER_NAME", "SYNAPSE_REPORT_STATS"))
  47. generate_secrets(environ, {
  48. "registration": "SYNAPSE_REGISTRATION_SHARED_SECRET",
  49. "macaroon": "SYNAPSE_MACAROON_SECRET_KEY"
  50. })
  51. environ["SYNAPSE_APPSERVICES"] = glob.glob("/data/appservices/*.yaml")
  52. if not os.path.exists("/compiled"): os.mkdir("/compiled")
  53. config_path = "/compiled/homeserver.yaml"
  54. # Convert SYNAPSE_NO_TLS to boolean if exists
  55. if "SYNAPSE_NO_TLS" in environ:
  56. tlsanswerstring = str.lower(environ["SYNAPSE_NO_TLS"])
  57. if tlsanswerstring in ("true", "on", "1", "yes"):
  58. environ["SYNAPSE_NO_TLS"] = True
  59. else:
  60. if tlsanswerstring in ("false", "off", "0", "no"):
  61. environ["SYNAPSE_NO_TLS"] = False
  62. else:
  63. print("Environment variable \"SYNAPSE_NO_TLS\" found but value \"" + tlsanswerstring + "\" unrecognized; exiting.")
  64. sys.exit(2)
  65. convert("/conf/homeserver.yaml", config_path, environ)
  66. convert("/conf/log.config", "/compiled/log.config", environ)
  67. subprocess.check_output(["chown", "-R", ownership, "/data"])
  68. args += [
  69. "--config-path", config_path,
  70. # tell synapse to put any generated keys in /data rather than /compiled
  71. "--keys-directory", "/data",
  72. ]
  73. # Generate missing keys and start synapse
  74. subprocess.check_output(args + ["--generate-keys"])
  75. os.execv("/sbin/su-exec", ["su-exec", ownership] + args)