54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
from flask import Flask, render_template
|
|
from flask_apscheduler import APScheduler
|
|
import docker
|
|
from pprint import pprint
|
|
import time
|
|
|
|
client = docker.from_env()
|
|
app = Flask(__name__)
|
|
scheduler = APScheduler()
|
|
|
|
|
|
@app.route("/", methods=['GET'])
|
|
def index():
|
|
containers = client.containers.list(all=True)
|
|
return render_template("index.html", title="test", containers_len=len(containers), containers=containers)
|
|
|
|
@app.route("/parts/modal/container/<container_id>", methods=['GET'])
|
|
def parts_container_modal(container_id):
|
|
container = client.containers.get(container_id)
|
|
|
|
project = ''
|
|
if('com.docker.compose.project' in container.attrs['Config']['Labels']):
|
|
project = container.attrs['Config']['Labels']['com.docker.compose.project']
|
|
|
|
return render_template("container_modal.html", name=container.name, project=project, ports=container.attrs['NetworkSettings']['Ports'], status=container.status)
|
|
|
|
@app.route("/api/<container_id>/<action>", methods=['DELETE', 'POST'])
|
|
def api_stop_container(container_id, action):
|
|
container = client.containers.get(container_id)
|
|
#pprint(vars(container))
|
|
try:
|
|
match action:
|
|
case 'stop':
|
|
container.stop()
|
|
case 'start':
|
|
container.start()
|
|
case _:
|
|
return 'False'
|
|
|
|
container = client.containers.get(container_id)
|
|
return container.status
|
|
except:
|
|
return 'False'
|
|
|
|
|
|
|
|
def scheduleTasks():
|
|
print("This test runs every 3 seconds")
|
|
|
|
if __name__ == '__main__':
|
|
#scheduler.add_job(id = 'scheduled_tasks', func=scheduleTasks, trigger="interval", seconds=3)
|
|
#scheduler.start()
|
|
app.run(host="0.0.0.0", port=99, debug=True)
|