diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..ad7d5bf --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,45 @@ +name: Build and Push Docker Image + +on: + push: + branches: + - main + tags: + - 'v*.*.*' + +jobs: + build-and-push: + name: Build image & push + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + # Step 1: Check out the repository + - name: Checkout code + uses: actions/checkout@v3 + + # Step 2: Extract version from __version__.py + - name: Extract version + run: | + VERSION=$(python -c "exec(open('__version__.py').read()); print(__version__)") + echo "Version is $VERSION" + echo "VERSION=$VERSION" >> $GITHUB_ENV + + # Step 3: Log in to GitHub Container Registry + - name: Log in to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Step 4: Build the Docker image + - name: Build Docker image + run: | + docker build -t ghcr.io/${{ github.repository }}:${{ env.VERSION }} . + + # Step 5: Push the Docker image to GHCR + - name: Push Docker image + run: | + docker push ghcr.io/${{ github.repository }}:${{ env.VERSION }} diff --git a/.gitignore b/.gitignore index d82269f..5ffda7e 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,5 @@ cython_debug/ # IDE files .idea/ + +config.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f8625d1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +# Use an official Python runtime as a parent image +FROM python:3.11-slim-buster + +# permissions and nonroot user for tightened security +RUN adduser --disabled-login nonroot +RUN mkdir /home/app/ && chown -R nonroot:nonroot /home/app +RUN mkdir -p /var/log/flask-app && touch /var/log/flask-app/flask-app.err.log && touch /var/log/flask-app/flask-app.out.log +RUN chown -R nonroot:nonroot /var/log/flask-app +WORKDIR /home/app +USER nonroot + +# copy all the files to the container +COPY --chown=nonroot:nonroot . . + +# venv +ENV VIRTUAL_ENV=/home/app/venv + +# python setup +RUN python -m venv $VIRTUAL_ENV +ENV PATH="$VIRTUAL_ENV/bin:$PATH" +RUN export FLASK_APP=src/app.py +RUN pip install --no-cache-dir -r requirements.txt + +# define the port number the container should expose +EXPOSE 8000 + +CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:app"] diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..4943a30 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Filip Bednárik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index d465b1f..e02d009 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,6 @@ TBD: - Filament spending based on printing - Evidently needed GUI improvements - Code cleanup - - Deployment as Docker and Helm chart + - Deployment as Helm chart - Video showcase - TODOs diff --git a/__version__.py b/__version__.py new file mode 100644 index 0000000..b5cb49c --- /dev/null +++ b/__version__.py @@ -0,0 +1 @@ +__version__ = '0.0.1' diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..886b710 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,6 @@ +services: + openspoolman: + build: . + env_file: "config.env" + ports: + - "8000:8000" diff --git a/config.env.template b/config.env.template new file mode 100644 index 0000000..e776d5a --- /dev/null +++ b/config.env.template @@ -0,0 +1,5 @@ +OPENSPOOLMAN_BASE_URL= +PRINTER_ACCESS_CODE= +PRINTER_ID= +PRINTER_IP= +SPOOLMAN_BASE_URL= diff --git a/mqtt_bambulab.py b/mqtt_bambulab.py index 288db0a..c57d19e 100644 --- a/mqtt_bambulab.py +++ b/mqtt_bambulab.py @@ -9,6 +9,9 @@ from config import PRINTER_ID, PRINTER_CODE, PRINTER_IP from messages import GET_VERSION, PUSH_ALL from spoolman_client import fetchSpoolList, patchExtraTags +MQTT_CLIENT = {} # Global variable storing MQTT Client +LAST_AMS_CONFIG = {} # Global variable storing last AMS configuration + def num2letter(num): return chr(ord("A") + int(num)) @@ -31,7 +34,7 @@ def on_message(client, userdata, msg): # TODO: Consume spool try: data = json.loads(msg.payload.decode()) - #print(data) + # print(data) if "print" in data and "vt_tray" in data["print"]: print(data) LAST_AMS_CONFIG["vt_tray"] = data["print"]["vt_tray"] @@ -82,7 +85,8 @@ def setActiveTray(spool_id, spool_extra, ams_id, tray_id): if spool_extra == None: spool_extra = {} - if not spool_extra.get("active_tray") or spool_extra.get("active_tray") != json.dumps(f"{PRINTER_ID}_{ams_id}_{tray_id}"): + if not spool_extra.get("active_tray") or spool_extra.get("active_tray") != json.dumps( + f"{PRINTER_ID}_{ams_id}_{tray_id}"): patchExtraTags(spool_id, spool_extra, { "active_tray": json.dumps(f"{PRINTER_ID}_{ams_id}_{tray_id}"), }) @@ -133,6 +137,4 @@ def getMqttClient(): return MQTT_CLIENT -MQTT_CLIENT = {} # Global variable storing MQTT Client -LAST_AMS_CONFIG = {} # Global variable storing last AMS configuration SPOOLS = fetchSpools() # Global variable storing latest spool from spoolman diff --git a/requirements.txt b/requirements.txt index d73bc56..9697a21 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ paho-mqtt==2.1.0 requests==2.32.3 flask==3.1.0 pyopenssl==24.3.0 +gunicorn==23.0.0