synctl.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2014-2016 OpenMarket Ltd
  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 sys
  17. import os
  18. import os.path
  19. import subprocess
  20. import signal
  21. import yaml
  22. SYNAPSE = ["python", "-B", "-m", "synapse.app.homeserver"]
  23. GREEN = "\x1b[1;32m"
  24. RED = "\x1b[1;31m"
  25. NORMAL = "\x1b[m"
  26. def start(configfile):
  27. print ("Starting ...")
  28. args = SYNAPSE
  29. args.extend(["--daemonize", "-c", configfile])
  30. try:
  31. subprocess.check_call(args)
  32. print (GREEN + "started" + NORMAL)
  33. except subprocess.CalledProcessError as e:
  34. print (
  35. RED +
  36. "error starting (exit code: %d); see above for logs" % e.returncode +
  37. NORMAL
  38. )
  39. def stop(pidfile):
  40. if os.path.exists(pidfile):
  41. pid = int(open(pidfile).read())
  42. os.kill(pid, signal.SIGTERM)
  43. print (GREEN + "stopped" + NORMAL)
  44. def main():
  45. configfile = sys.argv[2] if len(sys.argv) == 3 else "homeserver.yaml"
  46. if not os.path.exists(configfile):
  47. sys.stderr.write(
  48. "No config file found\n"
  49. "To generate a config file, run '%s -c %s --generate-config"
  50. " --server-name=<server name>'\n" % (
  51. " ".join(SYNAPSE), configfile
  52. )
  53. )
  54. sys.exit(1)
  55. config = yaml.load(open(configfile))
  56. pidfile = config["pid_file"]
  57. action = sys.argv[1] if sys.argv[1:] else "usage"
  58. if action == "start":
  59. start(configfile)
  60. elif action == "stop":
  61. stop(pidfile)
  62. elif action == "restart":
  63. stop(pidfile)
  64. start(configfile)
  65. else:
  66. sys.stderr.write("Usage: %s [start|stop|restart] [configfile]\n" % (sys.argv[0],))
  67. sys.exit(1)
  68. if __name__ == "__main__":
  69. main()