Skip to content

Commit

Permalink
Fix configuration file handling and service unit file creation, remov…
Browse files Browse the repository at this point in the history
…e unnecessary files
  • Loading branch information
AttilaGombosER committed Sep 12, 2024
1 parent 3143582 commit 0c24319
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 120 deletions.
3 changes: 3 additions & 0 deletions .fpm
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
--depends python3-tz
--python-disable-dependency pybase64
--depends python3-unpaddedbase64
--deb-systemd service/sentinel_mrhat_cam.service
--deb-systemd-enable
--deb-systemd-auto-start
31 changes: 0 additions & 31 deletions .github/workflows/python_release.yml

This file was deleted.

60 changes: 60 additions & 0 deletions .github/workflows/test_and_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Test and Release

on:
push:
branches: main
tags: v*.*.*

pull_request:
branches: [ "main" ]
types:
- synchronize
- opened
- reopened

concurrency:
group: ${{ github.workflow }}-${{ github.sha }}
cancel-in-progress: true

jobs:
test:
name: Build and test

runs-on: ubuntu-latest

permissions:
# Gives the action the necessary permissions for publishing new
# comments in pull requests.
pull-requests: write
contents: write

steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Verify changes
uses: EffectiveRange/python-verify-github-action@v1
with:
coverage-threshold: '0'

release:
if: startsWith(github.ref, 'refs/tags/')
needs: test

name: Publish and release

runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
submodules: true
- name: Package and publish
uses: EffectiveRange/python-package-github-action@v2
with:
use-devcontainer: 'true'
container-config: 'amd64-container'
debian-dist-type: 'fpm-deb'
install-packaging-tools: 'false'
- name: Release
uses: EffectiveRange/version-release-github-action@v1
File renamed without changes.
File renamed without changes.
33 changes: 0 additions & 33 deletions scripts/daemon.sh

This file was deleted.

8 changes: 1 addition & 7 deletions scripts/sentinel_mrhat_cam.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
#!/bin/bash

# Set the working directory
cd /home/admin

# Ensure the correct Python environment is used
export PATH="/usr/local/bin:$PATH"

# Configuration
RESTART_COUNT_FILE="/tmp/restart_count"

Expand All @@ -21,7 +15,7 @@ while true; do
start_time=$(date +%s)
echo "Starting Python script at $(date)"
# Run the Python script
python3 -m sentinel_mrhat_cam.main
python3 sentinel_mrhat_cam_main.py
EXIT_CODE=$?

end_time=$(date +%s)
Expand Down
46 changes: 43 additions & 3 deletions sentinel_mrhat_cam/main.py → scripts/sentinel_mrhat_cam_main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from .app import App
from .logger import Logger
from .static_config import LOG_CONFIG_PATH, CONFIG_PATH
#!/usr/bin/env python3

import shutil
from os import makedirs
from os.path import dirname, exists, isdir, join
from pathlib import Path

from sentinel_mrhat_cam.app import App
from sentinel_mrhat_cam.logger import Logger
from sentinel_mrhat_cam.static_config import LOG_CONFIG_PATH, CONFIG_PATH, CONFIG_DIR
import logging
import sys

Expand Down Expand Up @@ -34,6 +41,9 @@ def main():
It sets up all necessary components and manages the main execution flow.
"""

# Setting up the configuration directory and copying the default configuration files if necessary
_set_up_configuration()

# Configuring and starting the logging
logger = Logger(LOG_CONFIG_PATH)
logger.start_logging()
Expand Down Expand Up @@ -61,5 +71,35 @@ def main():
logger.disconnect_mqtt()


def _set_up_configuration():
"""
Set up the configuration directory and copy the default configuration files if necessary.
This function creates the configuration directory if it does not exist and copies the default
configuration files to the configuration directory if they do not exist. Will not overwrite existing files.
Notes
-----
This function is called before the main function to ensure that the configuration files are
available before the application starts.
"""

# Setting the default configuration path
default_config_dir = str(Path(dirname(__file__)).parent.absolute().joinpath('config'))

# Ensuring configuration directory exists
if not isdir(CONFIG_DIR):
makedirs(dirname(CONFIG_DIR), exist_ok=True)

# Copying the default configuration files to the config directory, if they do not exist
if not exists(LOG_CONFIG_PATH):
default_log_config = join(default_config_dir, 'log_config.yaml')
shutil.copy(default_log_config, LOG_CONFIG_PATH)

if not exists(CONFIG_PATH):
default_config = join(default_config_dir, 'config.json')
shutil.copy(default_config, CONFIG_PATH)


if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion sentinel_mrhat_cam/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,3 @@
from .camera import *
from .transmit import *
from .app import *
from .main import *
14 changes: 0 additions & 14 deletions sentinel_mrhat_cam/sentinel_mrhat_cam.egg-info/PKG-INFO

This file was deleted.

9 changes: 0 additions & 9 deletions sentinel_mrhat_cam/sentinel_mrhat_cam.egg-info/SOURCES.txt

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions sentinel_mrhat_cam/sentinel_mrhat_cam.egg-info/requires.txt

This file was deleted.

This file was deleted.

10 changes: 5 additions & 5 deletions sentinel_mrhat_cam/static_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import logging

# Configuration file paths
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_CONFIG_PATH = os.path.join(SCRIPT_DIR, 'log_config.yaml')
CONFIG_PATH = os.path.join(SCRIPT_DIR, 'config.json')
TEMP_CONFIG_PATH = os.path.join(SCRIPT_DIR, 'temp_config.json')
STATE_FILE_PATH = os.path.join(SCRIPT_DIR, 'state_file.json')
CONFIG_DIR = '/etc/sentinel_mrhat_cam'
LOG_CONFIG_PATH = os.path.join(CONFIG_DIR, 'log_config.yaml')
CONFIG_PATH = os.path.join(CONFIG_DIR, 'config.json')
TEMP_CONFIG_PATH = os.path.join(CONFIG_DIR, 'temp_config.json')
STATE_FILE_PATH = os.path.join(CONFIG_DIR, 'state_file.json')

# MQTT Configuration
""" BROKER = "192.168.0.105"
Expand Down
12 changes: 12 additions & 0 deletions service/sentinel_mrhat_cam.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=Run Script Daemon

[Service]
Type=simple
User=admin
ExecStart=/bin/bash /usr/local/bin/sentinel_mrhat_cam.sh
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
10 changes: 3 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
author='Ferenc Nandor Janky, Attila Gombos, Nyiri Levente, Nyitrai Bence',
author_email='[email protected]',
packages=find_packages(),
scripts=['scripts/sentinel_mrhat_cam.sh', 'scripts/daemon.sh'],
install_requires=['PyYAML>=6.0',
'pillow',
'pytz',
'paho-mqtt',
'numpy',
'pybase64']
scripts=['scripts/sentinel_mrhat_cam.sh', 'scripts/sentinel_mrhat_cam_main.py'],
data_files=[('config', ['config/config.json', 'config/log_config.yaml'])],
install_requires=['PyYAML>=6.0', 'pillow', 'pytz', 'paho-mqtt', 'numpy', 'pybase64'],
)

0 comments on commit 0c24319

Please sign in to comment.