synctl.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2014, 2015 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 subprocess
  19. import signal
  20. SYNAPSE = ["python", "-B", "-m", "synapse.app.homeserver"]
  21. CONFIGFILE = "homeserver.yaml"
  22. PIDFILE = "homeserver.pid"
  23. GREEN = "\x1b[1;32m"
  24. NORMAL = "\x1b[m"
  25. def start():
  26. if not os.path.exists(CONFIGFILE):
  27. sys.stderr.write(
  28. "No config file found\n"
  29. "To generate a config file, run '%s -c %s --generate-config"
  30. " --server-name=<server name>'\n" % (
  31. " ".join(SYNAPSE), CONFIGFILE
  32. )
  33. )
  34. sys.exit(1)
  35. print "Starting ...",
  36. args = SYNAPSE
  37. args.extend(["--daemonize", "-c", CONFIGFILE, "--pid-file", PIDFILE])
  38. subprocess.check_call(args)
  39. print GREEN + "started" + NORMAL
  40. def stop():
  41. if os.path.exists(PIDFILE):
  42. pid = int(open(PIDFILE).read())
  43. os.kill(pid, signal.SIGTERM)
  44. print GREEN + "stopped" + NORMAL
  45. def main():
  46. action = sys.argv[1] if sys.argv[1:] else "usage"
  47. if action == "start":
  48. start()
  49. elif action == "stop":
  50. stop()
  51. elif action == "restart":
  52. stop()
  53. start()
  54. else:
  55. sys.stderr.write("Usage: %s [start|stop|restart]\n" % (sys.argv[0],))
  56. sys.exit(1)
  57. if __name__ == "__main__":
  58. main()