From 7802d7ace265018487ddb68bfab96ce3a141ab3e Mon Sep 17 00:00:00 2001 From: Igor Bari Date: Sun, 17 Mar 2024 13:44:51 +0000 Subject: [PATCH] simplification --- scripts/build_test_image.py | 16 +++------------- src/ansible_utils.py | 15 ++------------- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/scripts/build_test_image.py b/scripts/build_test_image.py index dadb94d..5a40d32 100644 --- a/scripts/build_test_image.py +++ b/scripts/build_test_image.py @@ -10,23 +10,13 @@ from src.docker_utils import build_and_push_docker_img root_directory = Path(__file__).parent.parent -access_token = sys.argv[1] -vault_key = sys.argv[2] - -with NamedTemporaryFile() as vault_key_file: - vault_key_file.write(vault_key.encode()) - vault_key_file.flush() - data = load_vars(vault_key_file.name, root_directory / 'vars/vault.yaml') - -if not access_token: - print("GitHub access token is missing", flush=True, file=sys.stderr) - exit(1) +secrets = load_vars(sys.argv[2], root_directory / 'vars/vault.yaml') build_and_push_docker_img( src="src", tag_prefix="docker-image", image_name="publish-tools-test", docker_username=environ.get('GITHUB_REPOSITORY_OWNER'), - docker_password=data['docker_password'], - github_access_token=access_token + docker_password=secrets['docker_password'], + github_access_token=sys.argv[1] ) diff --git a/src/ansible_utils.py b/src/ansible_utils.py index e857295..c551599 100644 --- a/src/ansible_utils.py +++ b/src/ansible_utils.py @@ -1,5 +1,3 @@ -import sys - from os import makedirs, path from pathlib import Path from secrets import choice @@ -9,19 +7,10 @@ from ansible.parsing.dataloader import DataLoader -def read_file(file_path: Path): - try: - with open(file_path, 'rb') as file: - return file.read().strip() - except: - print("Error reading file at", file_path, flush=True, file=sys.stderr) - - -def load_vars(vault_secret_file: Path, vars_file: Path): - vault_secret = read_file(vault_secret_file) +def load_vars(vault_secret: str, vars_file: Path): loader = DataLoader() loader.set_vault_secrets( - [(DEFAULT_VAULT_ID_MATCH, VaultSecret(vault_secret))]) + [(DEFAULT_VAULT_ID_MATCH, VaultSecret(vault_secret.encode()))]) return loader.load_from_file(str(vars_file))