Skip to content

Commit

Permalink
Add --no-preserve option to cleanup workdir
Browse files Browse the repository at this point in the history
Add test for this case
Add new testing function (check_file_startingwith_not_in_dir) in testing lib
  • Loading branch information
LegenJCdary committed May 20, 2022
1 parent 5b66f1a commit 72f57e0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import datetime
import errno
import pkg_resources
from shutil import rmtree
from ansible_deployer.modules.globalvars import SUBCOMMANDS
from ansible_deployer.modules.configs.config import Config
from ansible_deployer.modules.locking.locking import Locking
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
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
}

0 comments on commit 72f57e0

Please sign in to comment.