From 42ebf48979594bc0b6aeb244198485af99944e4e Mon Sep 17 00:00:00 2001 From: LegenJCdary Date: Thu, 24 Mar 2022 06:17:33 -0400 Subject: [PATCH] Improve handling of invalid workdir creation --- .circleci/config.yml | 37 +++++++++++++++++- ansible_deployer/command_line.py | 43 ++++++++++++--------- tests/06-error_create_parent_workdir.sh | 9 +++++ tests/06-error_create_workdir.sh | 10 +++++ tests/06-error_list_workdir.sh | 9 +++++ tests/files/etc/ansible-deploy_workdir.yaml | 10 +++++ 6 files changed, 98 insertions(+), 20 deletions(-) create mode 100755 tests/06-error_create_parent_workdir.sh create mode 100755 tests/06-error_create_workdir.sh create mode 100755 tests/06-error_list_workdir.sh create mode 100644 tests/files/etc/ansible-deploy_workdir.yaml diff --git a/.circleci/config.yml b/.circleci/config.yml index 0856fa26..36566a4d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -15,7 +15,7 @@ jobs: - run: command: dnf install -y epel-release - run: - command: dnf install -y ansible python3 python3-pip + command: dnf install -y ansible python3 python3-pip e2fsprogs - checkout - run: name: "Install ansible-deployer" @@ -30,12 +30,46 @@ jobs: gpasswd -a root test_group groups /bin/bash -lc 'groups' + - run: + name: "costam" + command: cat /etc/passwd + + run_tests: + docker: + - image: almalinux + user: test_user + resource_class: small + steps: + - checkout + - run: + command: sudo dnf install -y ansible python3 + - run: + name: "Install ansible-deployer" + command: "pip3.6 install ." + - run: + name: "Copy configuration files" + command: cp -r ./etc /etc/ansible-deploy - run: name: "Run shell script for argument parsing" command: | ./tests/01-test_argument_parsing.sh ./tests/02-checkrun.sh ./tests/05-permission_checks.sh + - run: + name: "Create invalid workdir" + command: | + cp ./tests/files/etc/ansible-deploy_workdir.yaml /etc/ansible-deploy/ansible-deploy.yaml + mkdir /tmp/workdir + chmod 0400 /tmp/workdir + - run: + name: "Workdir not writable tests" + user: nobody + command: | + chmod 0400 /tmp/workdir + ./tests/06-error_create_workdir.sh + - run: + name: "Reset global configuration" + command: cp ./etc/ansible-deploy.yaml /etc/ansible-deploy/ - run: name: "Copy failing hooks" command: cp ./tests/files/etc/hooks/* /etc/ansible-deploy/hooks @@ -87,6 +121,7 @@ workflows: tests-without-ansible-playbook: jobs: - prepare_env + - run_tests # - install_and_exec: # requires: # - prepare_env diff --git a/ansible_deployer/command_line.py b/ansible_deployer/command_line.py index d462124f..d2b778ac 100644 --- a/ansible_deployer/command_line.py +++ b/ansible_deployer/command_line.py @@ -112,29 +112,34 @@ def create_workdir(timestamp: str): # #TODO: Add locking of the directory - if short_ts not in os.listdir(conf["global_paths"]["work_dir"]): - seq_path = os.path.join(date_dir, f"{conf['file_naming']['sequence_prefix']}0000") - try: - os.mkdir(date_dir) - os.chmod(date_dir, int(conf["permissions"]["parent_workdir"].split("o")[1], 8)) - logger.debug("Successfully created parent work dir: %s", seq_path) - except Exception as e: - logger.critical("Failed to create parent work dir: %s error was: %s", seq_path, e, - file=sys.stderr) - sys.exit(90) - else: - sequence_list = os.listdir(date_dir) - sequence_list.sort() - new_sequence = int(sequence_list[-1].split(conf['file_naming']['sequence_prefix'])[1]) + 1 - seq_path = os.path.join(date_dir, f"{conf['file_naming']['sequence_prefix']}" - f"{new_sequence:04d}") + try: + if short_ts not in os.listdir(conf["global_paths"]["work_dir"]): + seq_path = os.path.join(date_dir, f"{conf['file_naming']['sequence_prefix']}0000") + try: + os.mkdir(date_dir) + os.chmod(date_dir, int(conf["permissions"]["parent_workdir"].split("o")[1], 8)) + logger.debug("Successfully created parent work dir: %s", date_dir) + except Exception as e: + logger.critical("Failed to create parent work dir: %s error was: %s", date_dir, e) + sys.exit(90) + else: + sequence_list = os.listdir(date_dir) + sequence_list.sort() + new_sequence = int(sequence_list[-1].split(conf['file_naming']['sequence_prefix'])[1])\ + + 1 + seq_path = os.path.join(date_dir, f"{conf['file_naming']['sequence_prefix']}" + f"{new_sequence:04d}") + + except Exception as exc: + logger.critical("Failed to list work dir due to: %s", exc) + sys.exit(91) try: os.mkdir(seq_path) os.chdir(seq_path) - except Exception as e: - logger.critical("Failed to create work dir: %s error was: %s", seq_path, e, file=sys.stderr) - sys.exit(91) + except Exception as exc: + logger.critical("Failed to create work dir: %s due to: %s", seq_path, exc) + sys.exit(92) logger.debug("Successfully created workdir: %s", seq_path) return seq_path diff --git a/tests/06-error_create_parent_workdir.sh b/tests/06-error_create_parent_workdir.sh new file mode 100755 index 00000000..bfb4a0a1 --- /dev/null +++ b/tests/06-error_create_parent_workdir.sh @@ -0,0 +1,9 @@ +#!/bin/bash -l + + +source ./tests/testing_lib.sh + +ls -ld /tmp/workdir +ansible-deployer run -t task_exec_bin_true -s prod -i testInfra -d + +check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra' '\[CRITICAL\]: Failed to create parent work dir' diff --git a/tests/06-error_create_workdir.sh b/tests/06-error_create_workdir.sh new file mode 100755 index 00000000..a19de831 --- /dev/null +++ b/tests/06-error_create_workdir.sh @@ -0,0 +1,10 @@ +#!/bin/bash -l + + +source ./tests/testing_lib.sh + +ls -ld /tmp/workdir +whoami +ansible-deployer run -t task_exec_bin_true -s prod -i testInfra -d + +check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra' '\[CRITICAL\]: Failed to create work dir' diff --git a/tests/06-error_list_workdir.sh b/tests/06-error_list_workdir.sh new file mode 100755 index 00000000..4ce56fca --- /dev/null +++ b/tests/06-error_list_workdir.sh @@ -0,0 +1,9 @@ +#!/bin/bash -l + + +source ./tests/testing_lib.sh + +ls -ld /tmp/workdir +ansible-deployer run -t task_exec_bin_true -s prod -i testInfra -d + +check_message_in_output 'ansible-deployer run -t task_exec_bin_true -s prod -i testInfra' '\[CRITICAL\]: Failed to list work dir due to' diff --git a/tests/files/etc/ansible-deploy_workdir.yaml b/tests/files/etc/ansible-deploy_workdir.yaml new file mode 100644 index 00000000..bebd122c --- /dev/null +++ b/tests/files/etc/ansible-deploy_workdir.yaml @@ -0,0 +1,10 @@ +global_paths: + work_dir: "/tmp/workdir" + config_dir: "/etc/ansible-deploy" + +file_naming: + log_file_name_frmt: "ansible-deploy_execution_{}.log" + sequence_prefix: "SEQ" + +permissions: + parent_workdir: "0o1750"