From 46c38748cf37ba13b24e1df94711381ce659a252 Mon Sep 17 00:00:00 2001 From: Jeff Laflamme Date: Thu, 27 Jun 2024 11:25:36 +0700 Subject: [PATCH] Added docker support --- Dockerfile | 14 ++++++++++++++ README.md | 16 ++++++++++++++-- config.ini => config.ini.sample | 2 +- config_init.py | 3 ++- db_operations.py | 3 ++- entrypoint.sh | 23 +++++++++++++++++++++++ 6 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 Dockerfile rename config.ini => config.ini.sample (97%) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b1acfee --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3.8 + +WORKDIR /app + +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +COPY . . + +RUN chmod +x /app/entrypoint.sh + +ENTRYPOINT ["/app/entrypoint.sh"] + +CMD ["python", "-u", "server.py"] diff --git a/README.md b/README.md index 35e6fef..fe92eb1 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,9 @@ source venv/bin/activate pip install -r requirements.txt ``` -5. Set up the configuration in `config.ini`: +5. Configuration + - Copy config/config.init.sample to config/config.ini + - Set up the configuration in `config.ini`: **[interface]** @@ -73,7 +75,7 @@ pip install -r requirements.txt ```ini [interface] type = serial -# port = /dev/ttyUSB0 +# port = /dev/ttyUSB0 # or /dev/ttyACM0 # hostname = 192.168.x.x [sync] @@ -90,6 +92,16 @@ python server.py Be sure you've followed the Python virtual environment steps above and activated it before running. +## Docker +The docker entrypoint.sh will automatically copy config.ini.sample to config.ini +if you want to edit config, make sure you add the config directory as a docker volume. + +```sh +docker run -it --name tc2bbsmesh -v tc2bbsmesh-config:/config/ -v tc2bbsmesh-data:/data/ --device=/dev/ttyACM0 tc2-bbs-mesh +``` + +Tested on Orange Pi 3 LTS (Armbian), but should work on all linux distros. + ## Features - **Mail System**: Send and receive mail messages. diff --git a/config.ini b/config.ini.sample similarity index 97% rename from config.ini rename to config.ini.sample index 3dc354f..b953fd1 100644 --- a/config.ini +++ b/config.ini.sample @@ -17,7 +17,7 @@ [interface] type = serial -# port = /dev/ttyUSB0 +# port = /dev/ttyACM0 # hostname = 192.168.x.x diff --git a/config_init.py b/config_init.py index bf3ddd7..e4fb775 100644 --- a/config_init.py +++ b/config_init.py @@ -2,11 +2,12 @@ import configparser import time import meshtastic.serial_interface import meshtastic.tcp_interface +import os import serial.tools.list_ports def initialize_config(): config = configparser.ConfigParser() - config.read('config.ini') + config.read(os.getenv('CONFIG_FILE', 'config/config.ini')) interface_type = config['interface']['type'] hostname = config['interface'].get('hostname', None) diff --git a/db_operations.py b/db_operations.py index 0d8aaa3..87631bb 100644 --- a/db_operations.py +++ b/db_operations.py @@ -1,4 +1,5 @@ import logging +import os import sqlite3 import threading import uuid @@ -16,7 +17,7 @@ thread_local = threading.local() def get_db_connection(): if not hasattr(thread_local, 'connection'): - thread_local.connection = sqlite3.connect('bulletins.db') + thread_local.connection = sqlite3.connect(os.getenv('DB_FILE', 'data/bulletins.db')) return thread_local.connection def initialize_database(): diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..1de2624 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,23 @@ +#!/bin/bash +set -e + +CONFIG_DIR=/app/config +CONFIG_FILE=$CONFIG_DIR/config.ini +SAMPLE_FILE=config.ini.sample + +# Create the config directory if it does not exist +mkdir -p $CONFIG_DIR + +# Check if the config file exists, if not, copy the sample file +if [ ! -f $CONFIG_FILE ]; then + if [ -f $SAMPLE_FILE ]; then + cp $SAMPLE_FILE $CONFIG_FILE + echo "Copied $SAMPLE_FILE to $CONFIG_FILE" + else + echo "Sample file $SAMPLE_FILE does not exist. Please provide a sample file." + fi +else + echo "Config file $CONFIG_FILE already exists." +fi + +exec "$@"