refactor
This commit is contained in:
parent
2a7ce05feb
commit
0a0af01000
|
@ -0,0 +1,30 @@
|
|||
*.pyc
|
||||
.vscode/
|
||||
.venv/
|
||||
*.log
|
||||
.idea/
|
||||
*.idea
|
||||
.git/
|
||||
|
||||
.dockerignore
|
||||
.gitignore
|
||||
README.md
|
||||
tests/
|
||||
docker/
|
||||
__pycache__/
|
||||
|
||||
app/__pycache__/
|
||||
|
||||
app/config/
|
||||
!app/config/config-template.ini
|
||||
|
||||
app/backups/
|
||||
!app/backups/.keep
|
||||
|
||||
app/logs/
|
||||
!/app/logs/.keep
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -6,7 +6,7 @@ __pycache__
|
|||
*.log
|
||||
.idea/
|
||||
*.idea
|
||||
/app/config/config.ini
|
||||
/app/config/dev_list.txt
|
||||
/app/backusp/
|
||||
!/app/backusp/.keep
|
||||
app/config/config.ini
|
||||
app/config/dev_list.txt
|
||||
app/backusp/
|
||||
!app/backups/.gitignore
|
10
README.md
10
README.md
|
@ -3,7 +3,15 @@
|
|||
Раз в сутки, в указанное время, подключается к сетевым устройствам.
|
||||
Отправляет коммит в удалённый репозиторий.
|
||||
|
||||
|
||||
Python 3.13
|
||||
Используются библиотеки:
|
||||
- "gitpython>=3.1.44" - для работы с гитом
|
||||
- "netmiko>=4.5.0" - для подключения к сетевым устройствам
|
||||
- "schedule>=1.2.2" - для периодического выполнения backup
|
||||
- "schedule>=1.2.2" - для периодического выполнения backup
|
||||
|
||||
|
||||
Для запуска.
|
||||
Переменные окружения (см. app/config/.env-template)
|
||||
app/config/config.ini
|
||||
Пример конфиг файла см. app/config/config-template.ini
|
|
@ -6,9 +6,7 @@ import os
|
|||
|
||||
|
||||
def default_logging(main_logging: logging) -> None:
|
||||
log_dir: str = os.path.join(
|
||||
os.path.dirname(os.path.abspath(os.path.dirname(__file__))), "logs"
|
||||
)
|
||||
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
|
||||
main_logging.basicConfig(
|
||||
level=20,
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
|
@ -29,9 +27,7 @@ default_logging(logging)
|
|||
|
||||
|
||||
def configure_loging(main_logging: logging) -> None:
|
||||
log_dir: str = os.path.join(
|
||||
os.path.dirname(os.path.abspath(os.path.dirname(__file__))), "logs"
|
||||
)
|
||||
log_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "logs")
|
||||
logging.info("configure logging")
|
||||
main_logging.basicConfig(
|
||||
level=cfg.log.console_lvl,
|
||||
|
@ -46,7 +42,7 @@ def configure_loging(main_logging: logging) -> None:
|
|||
|
||||
|
||||
def load_config() -> configparser.ConfigParser:
|
||||
cfg_dir: str = os.path.abspath(os.path.dirname(__file__))
|
||||
cfg_dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config")
|
||||
|
||||
logging.info("loading config")
|
||||
loaded_config: configparser.ConfigParser = configparser.ConfigParser(
|
||||
|
@ -79,9 +75,7 @@ class ConfigTelegram:
|
|||
|
||||
@dataclass
|
||||
class ConfigBcp:
|
||||
dir: str = os.path.join(
|
||||
os.path.dirname(os.path.abspath(os.path.dirname(__file__))), "backups"
|
||||
)
|
||||
dir: str = os.path.join(os.path.abspath(os.path.dirname(__file__)), "backups")
|
||||
file: str = config["bcp"].get("file")
|
||||
pattern: str = config["bcp"].get("pattern")
|
||||
start_at: str = config["bcp"].get("start_at")
|
|
@ -1,5 +0,0 @@
|
|||
from .config import cfg
|
||||
|
||||
__all__ = [
|
||||
"cfg",
|
||||
]
|
|
@ -1,5 +1,4 @@
|
|||
import os
|
||||
from git import Repo, InvalidGitRepositoryError, RemoteProgress, GitCommandError
|
||||
from git import Repo, InvalidGitRepositoryError, Git
|
||||
from config import cfg
|
||||
import logging as log
|
||||
|
||||
|
@ -32,6 +31,7 @@ def check_remote_repo(repo: Repo):
|
|||
|
||||
|
||||
def check_bcp_repo(path: str = cfg.bcp.dir) -> None:
|
||||
Git().config("--global", "--add", "safe.directory", path)
|
||||
log.info("check local repo %r", path)
|
||||
try:
|
||||
repo = Repo(path)
|
||||
|
@ -40,6 +40,7 @@ def check_bcp_repo(path: str = cfg.bcp.dir) -> None:
|
|||
check_remote_repo(repo)
|
||||
|
||||
except InvalidGitRepositoryError:
|
||||
|
||||
repo = Repo.init(path=path, initial_branch=cfg.git.branch)
|
||||
log.info("init repo in %r", path)
|
||||
with repo.config_writer() as config:
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
FROM python:3.13-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends git && rm -rf /var/lib/apt/lists/*
|
||||
ADD . .
|
||||
RUN pip install --upgrade pip
|
||||
RUN pip install uv
|
||||
RUN uv sync --no-dev
|
||||
WORKDIR /app
|
||||
CMD ["uv", "run", "main.py"]
|
|
@ -0,0 +1,17 @@
|
|||
version: '3.3'
|
||||
|
||||
services:
|
||||
|
||||
net-backup:
|
||||
image: git.sm8255082.ru/osnova/net-backup:1.0.0
|
||||
restart: always
|
||||
volumes:
|
||||
- ./logs:/app/logs
|
||||
- ./config:/app/config
|
||||
- ./backups:/app/backups
|
||||
environment:
|
||||
- NET_DEV_USER=admin
|
||||
- NET_DEV_DOMAIN=local
|
||||
- NET_DEV_PWD=password
|
||||
- GIT_USERNAME=git_user
|
||||
- GIT_TOKEN=bla-bla-lba
|
Loading…
Reference in New Issue