server.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from flask import Flask
  2. from flask_restful import Resource, Api
  3. from flask import jsonify
  4. from flask import request
  5. from threading import Thread
  6. import docker
  7. import signal
  8. import time
  9. import os
  10. import re
  11. import sys
  12. docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock', version='auto')
  13. app = Flask(__name__)
  14. api = Api(app)
  15. class containers_get(Resource):
  16. def get(self):
  17. containers = {}
  18. try:
  19. for container in docker_client.containers.list(all=True):
  20. containers.update({container.attrs['Id']: container.attrs})
  21. return containers
  22. except Exception as e:
  23. return jsonify(type='danger', msg=e)
  24. class container_get(Resource):
  25. def get(self, container_id):
  26. if container_id and container_id.isalnum():
  27. try:
  28. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  29. return container.attrs
  30. except Exception as e:
  31. return jsonify(type='danger', msg=e)
  32. else:
  33. return jsonify(type='danger', msg='no or invalid id defined')
  34. class container_post(Resource):
  35. def post(self, container_id, post_action):
  36. if container_id and container_id.isalnum() and post_action:
  37. if post_action == 'stop':
  38. try:
  39. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  40. container.stop()
  41. return jsonify(type='success', msg='command completed successfully')
  42. except Exception as e:
  43. return jsonify(type='danger', msg=e)
  44. elif post_action == 'start':
  45. try:
  46. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  47. container.start()
  48. return jsonify(type='success', msg='command completed successfully')
  49. except Exception as e:
  50. return jsonify(type='danger', msg=e)
  51. elif post_action == 'restart':
  52. try:
  53. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  54. container.restart()
  55. return jsonify(type='success', msg='command completed successfully')
  56. except Exception as e:
  57. return jsonify(type='danger', msg=e)
  58. elif post_action == 'exec':
  59. if not request.json or not 'cmd' in request.json:
  60. return jsonify(type='danger', msg='cmd is missing')
  61. if request.json['cmd'] == 'sieve_list' and request.json['username']:
  62. try:
  63. for container in docker_client.containers.list(filters={"id": container_id}):
  64. return container.exec_run(["/bin/bash", "-c", "/usr/local/bin/doveadm sieve list -u '" + request.json['username'].replace("'", "'\\''") + "'"], user='vmail')
  65. except Exception as e:
  66. return jsonify(type='danger', msg=e)
  67. elif request.json['cmd'] == 'sieve_print' and request.json['script_name'] and request.json['username']:
  68. try:
  69. for container in docker_client.containers.list(filters={"id": container_id}):
  70. return container.exec_run(["/bin/bash", "-c", "/usr/local/bin/doveadm sieve get -u '" + request.json['username'].replace("'", "'\\''") + "' '" + request.json['script_name'].replace("'", "'\\''") + "'"], user='vmail')
  71. except Exception as e:
  72. return jsonify(type='danger', msg=e)
  73. elif request.json['cmd'] == 'worker_password' and request.json['raw']:
  74. try:
  75. for container in docker_client.containers.list(filters={"id": container_id}):
  76. hash = container.exec_run(["/bin/bash", "-c", "/usr/bin/rspamadm pw -e -p '" + request.json['raw'].replace("'", "'\\''") + "'"], user='_rspamd')
  77. f = open("/access.inc", "w")
  78. f.write('enable_password = "' + re.sub('[^0-9a-zA-Z\$]+', '', hash.rstrip()) + '";\n')
  79. f.close()
  80. container.restart()
  81. return jsonify(type='success', msg='command completed successfully')
  82. except Exception as e:
  83. return jsonify(type='danger', msg=e)
  84. else:
  85. return jsonify(type='danger', msg='Unknown command')
  86. else:
  87. return jsonify(type='danger', msg='invalid action')
  88. else:
  89. return jsonify(type='danger', msg='invalid container id or missing action')
  90. class GracefulKiller:
  91. kill_now = False
  92. def __init__(self):
  93. signal.signal(signal.SIGINT, self.exit_gracefully)
  94. signal.signal(signal.SIGTERM, self.exit_gracefully)
  95. def exit_gracefully(self,signum, frame):
  96. self.kill_now = True
  97. def startFlaskAPI():
  98. app.run(debug=False, host='0.0.0.0', port='8080', threaded=True)
  99. api.add_resource(containers_get, '/containers/json')
  100. api.add_resource(container_get, '/containers/<string:container_id>/json')
  101. api.add_resource(container_post, '/containers/<string:container_id>/<string:post_action>')
  102. if __name__ == '__main__':
  103. api_thread = Thread(target=startFlaskAPI)
  104. api_thread.daemon = True
  105. api_thread.start()
  106. killer = GracefulKiller()
  107. while True:
  108. time.sleep(1)
  109. if killer.kill_now:
  110. break
  111. print "Stopping dockerapi-mailcow"