[Dockerapi] Return answers in json

master
André 2017-11-03 20:26:09 +01:00
parent 21e20f3786
commit a9f64a3472
1 changed files with 47 additions and 19 deletions

View File

@ -1,6 +1,7 @@
from flask import Flask from flask import Flask
from flask_restful import Resource, Api from flask_restful import Resource, Api
from flask import jsonify from flask import jsonify
from flask import request
from threading import Thread from threading import Thread
import docker import docker
import signal import signal
@ -13,17 +14,23 @@ api = Api(app)
class containers_get(Resource): class containers_get(Resource):
def get(self): def get(self):
containers = {} containers = {}
for container in docker_client.containers.list(all=True): try:
containers.update({container.attrs['Id']: container.attrs}) for container in docker_client.containers.list(all=True):
return containers containers.update({container.attrs['Id']: container.attrs})
return containers
except Exception as e:
return jsonify(type='danger', msg=e)
class container_get(Resource): class container_get(Resource):
def get(self, container_id): def get(self, container_id):
if container_id and container_id.isalnum(): if container_id and container_id.isalnum():
for container in docker_client.containers.list(all=True, filters={"id": container_id}): try:
return container.attrs for container in docker_client.containers.list(all=True, filters={"id": container_id}):
return container.attrs
except Exception as e:
return jsonify(type='danger', msg=e)
else: else:
return jsonify(message='No or invalid id defined') return jsonify(type='danger', msg='no or invalid id defined')
class container_post(Resource): class container_post(Resource):
def post(self, container_id, post_action): def post(self, container_id, post_action):
@ -32,30 +39,51 @@ class container_post(Resource):
try: try:
for container in docker_client.containers.list(all=True, filters={"id": container_id}): for container in docker_client.containers.list(all=True, filters={"id": container_id}):
container.stop() container.stop()
except: return jsonify(type='success', msg='command completed successfully')
return 'Error' except Exception as e:
else: return jsonify(type='danger', msg=e)
return 'OK'
elif post_action == 'start': elif post_action == 'start':
try: try:
for container in docker_client.containers.list(all=True, filters={"id": container_id}): for container in docker_client.containers.list(all=True, filters={"id": container_id}):
container.start() container.start()
except: return jsonify(type='success', msg='command completed successfully')
return 'Error' except Exception as e:
else: return jsonify(type='danger', msg=e)
return 'OK'
elif post_action == 'restart': elif post_action == 'restart':
try: try:
for container in docker_client.containers.list(all=True, filters={"id": container_id}): for container in docker_client.containers.list(all=True, filters={"id": container_id}):
container.restart() container.restart()
except: return jsonify(type='success', msg='command completed successfully')
return 'Error' except Exception as e:
return jsonify(type='danger', msg=e)
elif post_action == 'exec':
if not request.json or not 'cmd' in request.json:
return jsonify(type='danger', msg='cmd is missing')
if request.json['cmd'] == 'sieve_list' and request.json['username']:
try:
for container in docker_client.containers.list(filters={"id": container_id}):
return container.exec_run(["/bin/bash", "-c", "/usr/local/bin/doveadm sieve list -u '" + request.json['username'].replace("'", "'\\''") + "'"], user='vmail')
except Exception as e:
return jsonify(type='danger', msg=e)
elif request.json['cmd'] == 'sieve_print' and request.json['script_name'] and request.json['username']:
try:
for container in docker_client.containers.list(filters={"id": container_id}):
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')
except Exception as e:
return jsonify(type='danger', msg=e)
else: else:
return 'OK' return jsonify(type='danger', msg='Unknown command')
else: else:
return jsonify(message='Invalid action') return jsonify(type='danger', msg='invalid action')
else: else:
return jsonify(message='Invalid container id or missing action') return jsonify(type='danger', msg='invalid container id or missing action')
class GracefulKiller: class GracefulKiller:
kill_now = False kill_now = False