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

Improve handling of invalid workdir creation #67

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
37 changes: 36 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -87,6 +121,7 @@ workflows:
tests-without-ansible-playbook:
jobs:
- prepare_env
- run_tests
# - install_and_exec:
# requires:
# - prepare_env
Expand Down
43 changes: 24 additions & 19 deletions ansible_deployer/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add tests for those criticals. Maybe a second configuration file with a different workdir run from another .sh ?

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sam as above

sys.exit(92)
logger.debug("Successfully created workdir: %s", seq_path)
return seq_path

Expand Down
9 changes: 9 additions & 0 deletions tests/06-error_create_parent_workdir.sh
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 10 additions & 0 deletions tests/06-error_create_workdir.sh
Original file line number Diff line number Diff line change
@@ -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'
9 changes: 9 additions & 0 deletions tests/06-error_list_workdir.sh
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 10 additions & 0 deletions tests/files/etc/ansible-deploy_workdir.yaml
Original file line number Diff line number Diff line change
@@ -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"