Add env file support and pass through log level

This commit is contained in:
DasMoorhuhn 2024-08-28 23:48:27 +02:00
parent 4c6689007d
commit b461894696
6 changed files with 92 additions and 28 deletions

0
.env
View File

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ history.*
data/
*.json
*.iso
*.cow
*.cow
.env

10
example.env Normal file
View File

@ -0,0 +1,10 @@
BACKGROUND=
TIME_ZONE=
NAME=
INTERACTIVE=true
BUILD=true
API=true
DEBUG=INFO
MODE=1
LOOP=20
TIMEOUT=20

View File

@ -4,9 +4,12 @@ import logging
def get_logger(logger_name:str, log_file='gateway.log'):
logger_name = logger_name.replace('__', '')
DEBUG = os.getenv('DEBUG') == 'true'
debug_level = os.getenv('DEBUG').upper()
if debug_level not in ('CRITICAL', 'ERROR', 'WARNING', 'WARN', 'INFO', 'DEBUG', 'NOTSET', 'FATAL'):
print(f'Loglevel "{debug_level}" is not supported.')
exit(0)
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG) if DEBUG else logger.setLevel(logging.INFO)
logger.setLevel(logging.getLevelName(debug_level))
handler = logging.FileHandler(filename=f'data/{log_file}', encoding='utf-8', mode='a')
formatter = logging.Formatter('%(asctime)s|%(levelname)s|%(name)s|:%(message)s')
handler.setFormatter(formatter)

View File

@ -26,18 +26,20 @@ logger.debug(f"VERSION: {os.getenv('VERSION')}")
update_state = check_for_update()
print_state(update_state)
try:
if DOCKER:
logger.info('Running in Docker')
if DOCKER:
logger.info('Running in Docker')
try:INTERVAL = int(interval)
except:pass
try:INTERVAL = int(interval)
except:pass
try:TIMEOUT = int(timeout)
except:pass
try:TIMEOUT = int(timeout)
except:pass
if interval is None: log_to_json(start_discovery(timeout=TIMEOUT))
else:start_loop(INTERVAL, TIMEOUT)
if interval is None: log_to_json(start_discovery(timeout=TIMEOUT))
else:start_loop(INTERVAL, TIMEOUT)
else:
start_loop(interval=40)
else:
start_loop(interval=40)
except Exception as err:
logger.error(err)

View File

@ -3,18 +3,8 @@ CONTAINER="dasmoorhuhn/atc-mithermometer-gateway"
CONTAINER_NAME="ATC_MiThermometer_Gateway"
VOLUME=$(pwd)/data
BACKGROUND=""
TIME_ZONE=""
NAME=""
INTERACTIVE=false
BUILD=false
API=false
DEBUG=false
MODE=1
LOOP="0"
TIMEOUT="0"
HELP="USAGE: sh run_docker.sh [OPTIONS] \n
HELP="Using any command line argument except -d bypasses the .env file\n\n
USAGE: sh run_docker.sh [OPTIONS] \n
[ -d ] Run in Backgrund \n
[ -t | --tag ] Set a docker tag. Default: latest \n
[ -b | --build ] Build the image before running the container \n
@ -55,6 +45,43 @@ docker_run() {
check_for_devices_config
if [ "$SKIP_ENV" = true ]; then
echo "Skip env file"
ENV_EXISTS=false
BACKGROUND=""
TIME_ZONE=""
NAME=""
INTERACTIVE=true
BUILD=true
API=true
DEBUG="INFO"
MODE="1"
LOOP="40"
TIMEOUT="20"
else
if [ -e .env ]
then
echo Loading .env file
export $(cat .env | xargs)
ENV_EXISTS=true
else
echo No env file found
ENV_EXISTS=false
BACKGROUND=""
TIME_ZONE=""
NAME=""
INTERACTIVE=true
BUILD=true
API=true
DEBUG="INFO"
MODE="1"
LOOP="40"
TIMEOUT="20"
fi
fi
COMMAND="docker run $BACKGROUND"
COMMAND="$COMMAND --cap-add=SYS_ADMIN"
COMMAND="$COMMAND --cap-add=NET_ADMIN"
@ -65,6 +92,10 @@ docker_run() {
COMMAND="$COMMAND --volume=$VOLUME:/src/data"
COMMAND="$COMMAND --volume=$PWD/devices.yml:/src/devices.yml"
if [ "$ENV_EXISTS" = true ]; then
COMMAND="$COMMAND --env-file .env"
fi
if [ "$INTERACTIVE" = true ]; then
COMMAND="$COMMAND --interactive"
COMMAND="$COMMAND --tty"
@ -100,7 +131,7 @@ docker_run() {
COMMAND="$COMMAND --env NAME=$NAME"
fi
if [ "$DEBUG" = true ]; then
if [ "$DEBUG" = "DEBUG" ]; then
COMMAND="$COMMAND --env DEBUG=$DEBUG"
COMMAND="$COMMAND $CONTAINER:$TAG"
echo
@ -108,6 +139,7 @@ docker_run() {
echo
echo DEBUG MODE
else
COMMAND="$COMMAND --env DEBUG=$DEBUG"
COMMAND="$COMMAND $CONTAINER:$TAG"
fi
@ -120,53 +152,68 @@ docker_run() {
while [ "$1" != "" ]; do
case $1 in
-se | --skip-env-file )
SKIP_ENV=true
shift
;;
-d )
BACKGROUND="-d"
shift
;;
--debug )
DEBUG=true
shift
DEBUG=$1
SKIP_ENV=true
shift
;;
-a | --api)
API=true
SKIP_ENV=true
shift
;;
-b | --build )
BUILD=true
SKIP_ENV=true
shift
;;
-v | --volume )
shift
VOLUME=$1
SKIP_ENV=true
shift
;;
-n | --name )
shift
NAME=$1
SKIP_ENV=true
shift
;;
-m2 | --mesh-gateway)
MODE=2
SKIP_ENV=true
shift
;;
-tz | --timezone )
shift
TIME_ZONE=$1
SKIP_ENV=true
shift
;;
-to | --timeout )
shift
TIMEOUT=$1
SKIP_ENV=true
shift
;;
-t | --tag )
shift
TAG=$1
SKIP_ENV=true
shift
;;
-l | --loop )
shift
SKIP_ENV=true
firstchar=`echo $1 | cut -c1-1`
if [ "$firstchar" = "-" ]; then
LOOP=0
@ -179,6 +226,7 @@ while [ "$1" != "" ]; do
;;
-i | --interactive )
INTERACTIVE=true
SKIP_ENV=true
shift
;;
-h | --help )