From 78f90a867c2a97d607a21642fa66eaed8c4cd7cd Mon Sep 17 00:00:00 2001 From: Matthias Veit Date: Tue, 30 May 2023 17:04:48 +0200 Subject: [PATCH] [resotoworker][feat] Define home-directory files as dict (#1615) --- resotoworker/Makefile | 81 +++++++++++++++++++++++++++ resotoworker/resotoworker/__main__.py | 2 +- resotoworker/resotoworker/config.py | 23 ++++++-- resotoworker/test/test_args.py | 11 ++++ 4 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 resotoworker/Makefile diff --git a/resotoworker/Makefile b/resotoworker/Makefile new file mode 100644 index 000000000..fc3f280a7 --- /dev/null +++ b/resotoworker/Makefile @@ -0,0 +1,81 @@ +.PHONY: clean clean-test clean-pyc clean-build clean-env docs help setup test test-all coverage list-outdated install-latest +.DEFAULT_GOAL := help +.SILENT: clean clean-build clean-pyc clean-test setup + +define BROWSER_PYSCRIPT +import os, webbrowser, sys + +from urllib.request import pathname2url + +webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) +endef +export BROWSER_PYSCRIPT + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef +export PRINT_HELP_PYSCRIPT + +BROWSER := python -c "$$BROWSER_PYSCRIPT" + +help: + @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) + +clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts + +clean-build: ## remove build artifacts + rm -fr build/ + rm -fr out/ + rm -fr gen/ + rm -fr dist/ + rm -fr .eggs/ + rm -fr .hypothesis/ + rm -fr .mypy_cache/ + find . -name '*.egg-info' -exec rm -fr {} + + find . -name '*.egg' -exec rm -fr {} + + +clean-pyc: ## remove Python file artifacts + find . -name '*.pyc' -exec rm -f {} + + find . -name '*.pyo' -exec rm -f {} + + find . -name '*~' -exec rm -f {} + + find . -name '__pycache__' -exec rm -fr {} + + +clean-test: ## remove test and coverage artifacts + rm -fr .tox/ + rm -f .coverage + rm -fr htmlcov/ + rm -fr .pytest_cache + +clean-env: ## remove environment + rm -fr venv-pypy + +lint: ## static code analysis + black --line-length 120 --check resotoworker test + flake8 resotoworker + #pylint resotoworker + mypy --python-version 3.9 --strict --install-types --non-interactive resotoworker + +test: ## run tests quickly with the default Python + pytest + +test-all: ## run tests on every Python version with tox + tox + +coverage: ## check code coverage quickly with the default Python + coverage run --source resotoworker -m pytest + coverage combine + coverage report -m + coverage html + $(BROWSER) htmlcov/index.html + +list-outdated: + pip list --outdated + +install-latest: + pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U diff --git a/resotoworker/resotoworker/__main__.py b/resotoworker/resotoworker/__main__.py index 7ba75712b..9939b15a8 100644 --- a/resotoworker/resotoworker/__main__.py +++ b/resotoworker/resotoworker/__main__.py @@ -98,7 +98,7 @@ def main() -> None: plugin_loader.add_plugin_config(config) config.load_config() - write_files_to_home_dir(config.resotoworker.write_files_to_home_dir, write_utf8_file) + write_files_to_home_dir(config.resotoworker.all_files_in_home_dir(), write_utf8_file) def send_request(request: requests.Request) -> requests.Response: prepared = request.prepare() diff --git a/resotoworker/resotoworker/config.py b/resotoworker/resotoworker/config.py index 96c1abc9d..15a8104c2 100644 --- a/resotoworker/resotoworker/config.py +++ b/resotoworker/resotoworker/config.py @@ -5,8 +5,7 @@ from resotolib.baseplugin import BaseCollectorPlugin from resotolib.logger import log from attrs import define, field, frozen -from typing import ClassVar, Optional, List, Type - +from typing import ClassVar, Optional, List, Type, Dict _default_collectors: List[str] = [] @@ -105,12 +104,28 @@ class ResotoWorkerConfig: ) write_files_to_home_dir: List[HomeDirectoryFile] = field( factory=list, + metadata={ + "description": "Deprecated. Use files_in_home_dir instead.", + "restart_required": True, + }, + ) + # optional for backwards compatibility + files_in_home_dir: Optional[Dict[str, str]] = field( # type: ignore + factory=dict, metadata={ "description": ( - "All entries that are defined in this section are created as files on demand. " + "All entries that are defined in this section are created as files on demand.\n" "Use this option to define .aws/credentials, .kube/config file or other " - "credential files that should be passed to the worker as file." + "credential files that should be passed to the worker as file.\n" + "The key is the path to the file, the value is the content of the file." ), "restart_required": True, }, ) + + def all_files_in_home_dir(self) -> List[HomeDirectoryFile]: + files = self.write_files_to_home_dir.copy() + if self.files_in_home_dir is not None: + for path, content in self.files_in_home_dir.items(): + files.append(HomeDirectoryFile(path=path, content=content)) + return files diff --git a/resotoworker/test/test_args.py b/resotoworker/test/test_args.py index 7ce502ac2..347ea4ecd 100644 --- a/resotoworker/test/test_args.py +++ b/resotoworker/test/test_args.py @@ -1,6 +1,7 @@ from resotolib.args import ArgumentParser from resotoworker.__main__ import add_args from resotolib.core import add_args as core_add_args, resotocore +from resotoworker.config import ResotoWorkerConfig, HomeDirectoryFile def test_args() -> None: @@ -12,3 +13,13 @@ def test_args() -> None: core_add_args(arg_parser) arg_parser.parse_args() assert resotocore.http_uri == "https://localhost:8900" + + +def test_config() -> None: + cfg = ResotoWorkerConfig(write_files_to_home_dir=[HomeDirectoryFile(path="a", content="a")]) + assert cfg.all_files_in_home_dir() == [HomeDirectoryFile("a", "a")] + cfg = ResotoWorkerConfig( + write_files_to_home_dir=[HomeDirectoryFile(path="a", content="a")], + files_in_home_dir={"b": "b"}, + ) + assert cfg.all_files_in_home_dir() == [HomeDirectoryFile("a", "a"), HomeDirectoryFile("b", "b")]