[Docker API] Remove logs, remove env var for compose project name

master
andre.peters 2017-12-11 09:43:01 +01:00
parent f1d533eef6
commit 2994b94b6c
1 changed files with 9 additions and 20 deletions

View File

@ -18,7 +18,7 @@ class containers_get(Resource):
def get(self): def get(self):
containers = {} containers = {}
try: try:
for container in docker_client.containers.list(all=True, filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME']}): for container in docker_client.containers.list(all=True):
containers.update({container.attrs['Id']: container.attrs}) containers.update({container.attrs['Id']: container.attrs})
return containers return containers
except Exception as e: except Exception as e:
@ -28,30 +28,19 @@ 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():
try: try:
for container in docker_client.containers.list(all=True, filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}): for container in docker_client.containers.list(all=True, filters={"id": container_id}):
return container.attrs return container.attrs
except Exception as e: except Exception as e:
return jsonify(type='danger', msg=e) return jsonify(type='danger', msg=e)
else: else:
return jsonify(type='danger', msg='no or invalid id defined') return jsonify(type='danger', msg='no or invalid id defined')
class container_logs(Resource):
def get(self, container_id, lines):
if container_id and container_id.isalnum() and lines:
try:
for container in docker_client.containers.list(all=True, filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}):
return container.logs(stdout=True, stderr=True, stream=False, tail=lines)
except Exception as e:
return jsonify(type='danger', msg=e)
else:
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):
if container_id and container_id.isalnum() and post_action: if container_id and container_id.isalnum() and post_action:
if post_action == 'stop': if post_action == 'stop':
try: try:
for container in docker_client.containers.list(all=True, filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}): for container in docker_client.containers.list(all=True, filters={"id": container_id}):
container.stop() container.stop()
return jsonify(type='success', msg='command completed successfully') return jsonify(type='success', msg='command completed successfully')
except Exception as e: except Exception as e:
@ -59,7 +48,7 @@ class container_post(Resource):
elif post_action == 'start': elif post_action == 'start':
try: try:
for container in docker_client.containers.list(all=True, filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}): for container in docker_client.containers.list(all=True, filters={"id": container_id}):
container.start() container.start()
return jsonify(type='success', msg='command completed successfully') return jsonify(type='success', msg='command completed successfully')
except Exception as e: except Exception as e:
@ -67,7 +56,7 @@ class container_post(Resource):
elif post_action == 'restart': elif post_action == 'restart':
try: try:
for container in docker_client.containers.list(all=True, filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}): for container in docker_client.containers.list(all=True, filters={"id": container_id}):
container.restart() container.restart()
return jsonify(type='success', msg='command completed successfully') return jsonify(type='success', msg='command completed successfully')
except Exception as e: except Exception as e:
@ -80,19 +69,19 @@ class container_post(Resource):
if request.json['cmd'] == 'sieve_list' and request.json['username']: if request.json['cmd'] == 'sieve_list' and request.json['username']:
try: try:
for container in docker_client.containers.list(filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}): 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') return container.exec_run(["/bin/bash", "-c", "/usr/local/bin/doveadm sieve list -u '" + request.json['username'].replace("'", "'\\''") + "'"], user='vmail')
except Exception as e: except Exception as e:
return jsonify(type='danger', msg=e) return jsonify(type='danger', msg=e)
elif request.json['cmd'] == 'sieve_print' and request.json['script_name'] and request.json['username']: elif request.json['cmd'] == 'sieve_print' and request.json['script_name'] and request.json['username']:
try: try:
for container in docker_client.containers.list(filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}): 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') 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: except Exception as e:
return jsonify(type='danger', msg=e) return jsonify(type='danger', msg=e)
elif request.json['cmd'] == 'worker_password' and request.json['raw']: elif request.json['cmd'] == 'worker_password' and request.json['raw']:
try: try:
for container in docker_client.containers.list(filters={"label": "com.docker.compose.project=" + os.environ['COMPOSE_PROJECT_NAME'], "id": container_id}): for container in docker_client.containers.list(filters={"id": container_id}):
hash = container.exec_run(["/bin/bash", "-c", "/usr/bin/rspamadm pw -e -p '" + request.json['raw'].replace("'", "'\\''") + "'"], user='_rspamd') hash = container.exec_run(["/bin/bash", "-c", "/usr/bin/rspamadm pw -e -p '" + request.json['raw'].replace("'", "'\\''") + "'"], user='_rspamd')
f = open("/access.inc", "w") f = open("/access.inc", "w")
f.write('enable_password = "' + re.sub('[^0-9a-zA-Z\$]+', '', hash.rstrip()) + '";\n') f.write('enable_password = "' + re.sub('[^0-9a-zA-Z\$]+', '', hash.rstrip()) + '";\n')
@ -125,7 +114,6 @@ def startFlaskAPI():
api.add_resource(containers_get, '/containers/json') api.add_resource(containers_get, '/containers/json')
api.add_resource(container_get, '/containers/<string:container_id>/json') api.add_resource(container_get, '/containers/<string:container_id>/json')
api.add_resource(container_logs, '/containers/<string:container_id>/logs/<int:lines>')
api.add_resource(container_post, '/containers/<string:container_id>/<string:post_action>') api.add_resource(container_post, '/containers/<string:container_id>/<string:post_action>')
if __name__ == '__main__': if __name__ == '__main__':
@ -138,3 +126,4 @@ if __name__ == '__main__':
if killer.kill_now: if killer.kill_now:
break break
print "Stopping dockerapi-mailcow" print "Stopping dockerapi-mailcow"