From f107c705fbc05b3ad02250126b3bd5bb4d600fa5 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 9 Sep 2024 17:20:39 +0530 Subject: [PATCH] fix: Minimal app to toy with nerdctl --- pyproject.toml | 1 + worker/README.md | 5 +++++ worker/__init__.py | 0 worker/app.py | 19 +++++++++++++++++++ worker/container.py | 15 +++++++++++++++ 5 files changed, 40 insertions(+) create mode 100644 worker/README.md create mode 100644 worker/__init__.py create mode 100644 worker/app.py create mode 100644 worker/container.py diff --git a/pyproject.toml b/pyproject.toml index 7959bb3..666b9e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ dependencies = [ "cdktf==0.20.8", "cdktf-cdktf-provider-digitalocean==11.5.2", "containerd==1.5.3", + "Flask==3.0.3", ] [build-system] diff --git a/worker/README.md b/worker/README.md new file mode 100644 index 0000000..11d6fc9 --- /dev/null +++ b/worker/README.md @@ -0,0 +1,5 @@ +Run with + +```sh +flask --app app run --debug +``` \ No newline at end of file diff --git a/worker/__init__.py b/worker/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/worker/app.py b/worker/app.py new file mode 100644 index 0000000..1415555 --- /dev/null +++ b/worker/app.py @@ -0,0 +1,19 @@ +from flask import Flask, jsonify +from flask.views import MethodView + +from .container import Container + +application = Flask(__name__) + + +class ContainerAPI(MethodView): + init_every_request = False + + def get(self): + return jsonify(Container.list()) + + def post(self): + return jsonify(Container.create()) + + +application.add_url_rule("/containers/", view_func=ContainerAPI.as_view("containers")) diff --git a/worker/container.py b/worker/container.py new file mode 100644 index 0000000..c4ed7b0 --- /dev/null +++ b/worker/container.py @@ -0,0 +1,15 @@ +import json +import shlex +from subprocess import check_output + + +class Container: + @staticmethod + def list(): + command = shlex.split("nerdctl container ls --no-trunc --format '{{json .}}'") + return [json.loads(line) for line in check_output(command).splitlines()] + + @staticmethod + def create(): + command = shlex.split("nerdctl container run --detach redis:alpine") + return {"name": check_output(command).decode()}