server.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock')
  10. app = Flask(__name__)
  11. api = Api(app)
  12. class containers_get(Resource):
  13. def get(self):
  14. containers = {}
  15. try:
  16. for container in docker_client.containers.list(all=True):
  17. containers.update({container.attrs['Id']: container.attrs})
  18. return containers
  19. except Exception as e:
  20. return jsonify(type='danger', msg=e)
  21. class container_get(Resource):
  22. def get(self, container_id):
  23. if container_id and container_id.isalnum():
  24. try:
  25. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  26. return container.attrs
  27. except Exception as e:
  28. return jsonify(type='danger', msg=e)
  29. else:
  30. return jsonify(type='danger', msg='no or invalid id defined')
  31. class container_post(Resource):
  32. def post(self, container_id, post_action):
  33. if container_id and container_id.isalnum() and post_action:
  34. if post_action == 'stop':
  35. try:
  36. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  37. container.stop()
  38. return jsonify(type='success', msg='command completed successfully')
  39. except Exception as e:
  40. return jsonify(type='danger', msg=e)
  41. elif post_action == 'start':
  42. try:
  43. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  44. container.start()
  45. return jsonify(type='success', msg='command completed successfully')
  46. except Exception as e:
  47. return jsonify(type='danger', msg=e)
  48. elif post_action == 'restart':
  49. try:
  50. for container in docker_client.containers.list(all=True, filters={"id": container_id}):
  51. container.restart()
  52. return jsonify(type='success', msg='command completed successfully')
  53. except Exception as e:
  54. return jsonify(type='danger', msg=e)
  55. elif post_action == 'exec':
  56. if not request.json or not 'cmd' in request.json:
  57. return jsonify(type='danger', msg='cmd is missing')
  58. if request.json['cmd'] == 'sieve_list' and request.json['username']:
  59. try:
  60. for container in docker_client.containers.list(filters={"id": container_id}):
  61. return container.exec_run(["/bin/bash", "-c", "/usr/local/bin/doveadm sieve list -u '" + request.json['username'].replace("'", "'\\''") + "'"], user='vmail')
  62. except Exception as e:
  63. return jsonify(type='danger', msg=e)
  64. elif request.json['cmd'] == 'sieve_print' and request.json['script_name'] and request.json['username']:
  65. try:
  66. for container in docker_client.containers.list(filters={"id": container_id}):
  67. 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')
  68. except Exception as e:
  69. return jsonify(type='danger', msg=e)
  70. else:
  71. return jsonify(type='danger', msg='Unknown command')
  72. else:
  73. return jsonify(type='danger', msg='invalid action')
  74. else:
  75. return jsonify(type='danger', msg='invalid container id or missing action')
  76. class GracefulKiller:
  77. kill_now = False
  78. def __init__(self):
  79. signal.signal(signal.SIGINT, self.exit_gracefully)
  80. signal.signal(signal.SIGTERM, self.exit_gracefully)
  81. def exit_gracefully(self,signum, frame):
  82. self.kill_now = True
  83. def startFlaskAPI():
  84. app.run(debug=False, host='0.0.0.0', port='8080', threaded=True)
  85. api.add_resource(containers_get, '/containers/json')
  86. api.add_resource(container_get, '/containers/<string:container_id>/json')
  87. api.add_resource(container_post, '/containers/<string:container_id>/<string:post_action>')
  88. if __name__ == '__main__':
  89. api_thread = Thread(target=startFlaskAPI)
  90. api_thread.daemon = True
  91. api_thread.start()
  92. killer = GracefulKiller()
  93. while True:
  94. time.sleep(1)
  95. if killer.kill_now:
  96. break
  97. print "Stopping dockerapi-mailcow"