From 2dc03b3242e9683af019a2073fc6930c05513667 Mon Sep 17 00:00:00 2001 From: Richard Casey Date: Thu, 23 May 2024 08:35:32 -0600 Subject: [PATCH 1/2] modify loggerUtilVoterRoll.py for docker environment on Windows --- utilities/loggerUtilVoterRoll.py | 47 ++++++++++++++++---------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/utilities/loggerUtilVoterRoll.py b/utilities/loggerUtilVoterRoll.py index 248a610..2400349 100644 --- a/utilities/loggerUtilVoterRoll.py +++ b/utilities/loggerUtilVoterRoll.py @@ -4,8 +4,8 @@ import os import platform import sys -from logging.handlers import RotatingFileHandler -from pythonjsonlogger import jsonlogger +from logging.handlers import RotatingFileHandler +from pythonjsonlogger import jsonlogger # set logger name = module name logger = logging.getLogger(__name__) @@ -23,27 +23,28 @@ sys.exit(1) # File handler. date format is ISO-8601 -# rotate and delete log files -try: - # Identify the platform - if platform.system() == 'Windows': # Windows - logs_file = os.path.join(os.environ['USERPROFILE'], 'VoterRoll', 'logs', 'logs.txt') - else: - raise ValueError("ERROR: unsupported platform") - - fileHandler = RotatingFileHandler(logs_file, backupCount=5, maxBytes=50000000) # five log file rotations; rotate log filename at 50 Mb; delete oldest file - - fmtJson = jsonlogger.JsonFormatter( - "%(name)s %(asctime)s %(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", - rename_fields={"levelname": "severity", "asctime": "timestamp"}, - datefmt="%Y-%m-%dT%H:%M:%SZ", - ) # json file format - fileHandler.setFormatter(fmtJson) - fileHandler.setLevel(logging.DEBUG) # set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) - logger.addHandler(fileHandler) -except Exception as e: - print(f"ERROR: failed to set up file logging: {e}") - sys.exit(1) +# Only add file logging if not running inside Docker +if not os.environ.get('DOCKER_ENV'): + try: + # Identify the platform + if platform.system() == 'Windows': # Windows + logs_file = os.path.join(os.environ['USERPROFILE'], 'VoterRoll', 'logs', 'logs.txt') + else: + raise ValueError("ERROR: unsupported platform") + + fileHandler = RotatingFileHandler(logs_file, backupCount=5, maxBytes=50000000) # five log file rotations; rotate log filename at 50 Mb; delete oldest file + + fmtJson = jsonlogger.JsonFormatter( + "%(name)s %(asctime)s %(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", + rename_fields={"levelname": "severity", "asctime": "timestamp"}, + datefmt="%Y-%m-%dT%H:%M:%SZ", + ) # json file format + fileHandler.setFormatter(fmtJson) + fileHandler.setLevel(logging.DEBUG) # set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) + logger.addHandler(fileHandler) + except Exception as e: + print(f"ERROR: failed to set up file logging: {e}") + sys.exit(1) # Uncaught exceptions handler. # log these as CRITICAL. KeyboardInterrupt is treated as normal termination of the script. From 943b544aa80a183b21e9d233e28a6400bb702f40 Mon Sep 17 00:00:00 2001 From: Richard Casey Date: Thu, 23 May 2024 08:36:09 -0600 Subject: [PATCH 2/2] set up for docker --- .dockerignore | 11 +++++++++++ Dockerfile | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c54a91a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +__pycache__ +*.pyc +*.pyo +*.pyd +*.pdb +*.egg +*.egg-info +dist +build +.vscode +.idea diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..35bb293 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Python image from the Docker Hub +FROM python:3.12.3-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements file into the container +COPY requirements.txt . + +# Install the dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code into the container +COPY . . + +# Set environment variable to indicate Docker environment +ENV DOCKER_ENV=1 + +# Specify the command to run the application +CMD ["python", "VoterRoll.py"] \ No newline at end of file