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

Add --no-preserve option to cleanup workdir #136

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import datetime
import errno
from shutil import rmtree
import pkg_resources
from ansible_deployer.modules.globalvars import SUBCOMMANDS
from ansible_deployer.modules.configs.config import Config
Expand Down Expand Up @@ -49,6 +50,8 @@ def parse_options(argv):
help='Setup repo outside of workdir in requested path. This option applies'
' only to infrastructures with allow_user_checkout enabled in infra'
' config!')
parser.add_argument("--no-preserve", default=False, action="store_true", help='Remove workdir '
' after succesful execution.')

arguments = parser.parse_args(argv)

Expand Down Expand Up @@ -87,6 +90,7 @@ def parse_options(argv):
options["conf_dir"] = os.path.abspath(arguments.conf_dir[0])
else:
options["conf_dir"] = None
options["no_preserve"] = arguments.no_preserve

return options

Expand Down Expand Up @@ -143,6 +147,12 @@ def main():
lock.lock_inventory(lockdir, lockpath)
runner.run_playitem(config, options, inv_file, lockpath)
lock.unlock_inventory(lockpath)
if options["no_preserve"]:
try:
rmtree(workdir)
logger.logger.info("Working directory succesfully removed.")
except Exception as exc:
logger.logger.critical("Failed to remove working directory due to: %s.", exc)
elif options["subcommand"] == "lock":
lock.lock_inventory(lockdir, lockpath)
elif options["subcommand"] == "unlock":
Expand Down
5 changes: 5 additions & 0 deletions tests/02-checkrun.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ check_message_in_output 'ansible-deployer show task' 'Available tasks:'
check_run_ok "ansible-deployer run -t task_with_multi_groups -s testing -i testInfra"
check_message_in_output "ansible-deployer run -t task_with_multi_groups_fail -s testing -i testInfra" "\[CRITICAL\]: Task forbidden"

# Check --no-preserve option
check_run_ok "ansible-deployer run -t task_exec_bin_true -s prod -i testInfra --no-preserve"
search_path=$(find_latest_sequence)
check_file_startingwith_not_in_dir "$search_path" "ansible-deploy_execution_"

#Try execution of task without permissions
if [ $UID -ne 0 ]
then
Expand Down
4 changes: 2 additions & 2 deletions tests/pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ max-bool-expr=5
max-branches=16

# Maximum number of locals for function / method body.
max-locals=15
max-locals=16

# Maximum number of parents for a class (see R0901).
max-parents=7
Expand All @@ -533,7 +533,7 @@ max-public-methods=20
max-returns=6

# Maximum number of statements in function / method body.
max-statements=50
max-statements=54

# Minimum number of public methods for a class (see R0903).
min-public-methods=2
Expand Down
11 changes: 11 additions & 0 deletions tests/testing_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ check_file_startingwith_in_dir() {
exit 1
fi
}

check_file_startingwith_not_in_dir() {
eval "find $1 -name "${2}*" 1>/dev/null"
if [ $? -eq 0 ]
then
echo "FAILED: $1 has file starting with pattern $2"
else
echo "OK: $1 does not contain file starting with pattern $2"
exit 1
fi
}