Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argparse for config file and version, more config for log customi… #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ exclusions:
nodes: [] # Example: [px-3] or [px-3, px-4, px-8, px-9]

# Loguru settings
logging_level: 'INFO' # You can choose 'DEBUG" or "INFO" or "WARNING" or "ERROR"
logging:
level: 'DEBUG' # You can choose 'DEBUG" or "INFO" or "WARNING" or "ERROR"
rotation: '1 day' # If output is filename
retention: '2 days' # If output is filename
format: '{level} | {message}'
output: 'stdout' # Can be a file name or "stdout"

# Mail settings
mail:
Expand Down
28 changes: 16 additions & 12 deletions plb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Proxmox-load-balancer v0.6.4-betta Copyright (C) 2022 cvk98 (github.com/cvk98)
__author__ = 'github.com/cvk98'
__version__ = "0.6.5-betta"

import sys
import requests
Expand All @@ -14,9 +15,16 @@
from itertools import permutations
from copy import deepcopy
from loguru import logger
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-v', action='version', version='%(prog)s v{version} by {author}'.format(version=__version__, author=__author__))
parser.add_argument('-c',dest="filename", type=str, default="config.yaml", help='Yaml configuration file', metavar="FILE")

args = parser.parse_args()

try:
with open("config.yaml", "r", encoding='utf8') as yaml_file:
with open(args.filename, "r", encoding='utf8') as yaml_file:
cfg = yaml.safe_load(yaml_file)
except Exception as e:
print(f'Error opening the configuration file: {e}')
Expand Down Expand Up @@ -53,16 +61,12 @@

"""Loguru"""
logger.remove()
# For Linux service
logger.add(sys.stdout, format="{level} | {message}", level=cfg["logging_level"])

# For Windows and linux window mode (you can change sys.stdout to "file.log")
# logger.add(sys.stdout,
# colorize=True,
# format="<green>{time:YYYY-MM-DD at HH:mm:ss}</green> | "
# "<level>{level}</level> | "
# "<level>{message}</level>",
# level=cfg["logging_level"])
if cfg["logging"]["output"] == "stdout":
logger.add(sys.stdout,
level=cfg["logging"]["level"], format=cfg["logging"]["format"])
else:
logger.add(["logging"]["output"],
level=cfg["logging"]["level"], rotation=cfg["logging"]["rotation"], retention=cfg["logging"]["retention"], format=cfg["logging"]["format"])

"""Constants"""
GB = cfg["Gigabyte"]
Expand Down