From 9ccc0464ff5827927ac89c5a96291ffba3475be7 Mon Sep 17 00:00:00 2001 From: Amin Vakil Date: Thu, 19 Nov 2020 23:21:16 +0330 Subject: [PATCH] archive: remove path folder itself when remove paramater is true (#1083) * Remove paths folders themselves * Add changelog * Add remove integration test * Check excluded file --- .../1083-archive-remove-path-folder.yml | 3 + plugins/modules/files/archive.py | 7 + .../targets/archive/tasks/main.yml | 3 + .../targets/archive/tasks/remove.yml | 123 ++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 changelogs/fragments/1083-archive-remove-path-folder.yml create mode 100644 tests/integration/targets/archive/tasks/remove.yml diff --git a/changelogs/fragments/1083-archive-remove-path-folder.yml b/changelogs/fragments/1083-archive-remove-path-folder.yml new file mode 100644 index 0000000000..2a7ef76b86 --- /dev/null +++ b/changelogs/fragments/1083-archive-remove-path-folder.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - archive - remove path folder itself when ``remove`` paramater is true (https://github.com/ansible-collections/community.general/issues/1041). diff --git a/plugins/modules/files/archive.py b/plugins/modules/files/archive.py index 18970f4896..a7775dda95 100644 --- a/plugins/modules/files/archive.py +++ b/plugins/modules/files/archive.py @@ -453,6 +453,13 @@ def main(): except OSError as e: errors.append(to_native(b_path)) + for b_path in b_expanded_paths: + try: + if os.path.isdir(b_path): + shutil.rmtree(b_path) + except OSError as e: + errors.append(to_native(b_path)) + if errors: module.fail_json(dest=dest, msg='Error deleting some source files: ', files=errors) diff --git a/tests/integration/targets/archive/tasks/main.yml b/tests/integration/targets/archive/tasks/main.yml index 91de9fc516..7e9f198bf6 100644 --- a/tests/integration/targets/archive/tasks/main.yml +++ b/tests/integration/targets/archive/tasks/main.yml @@ -343,3 +343,6 @@ - name: Remove backports.lzma if previously installed (pip) pip: name=backports.lzma state=absent when: backports_lzma_pip is changed + +- name: import remove tests + import_tasks: remove.yml diff --git a/tests/integration/targets/archive/tasks/remove.yml b/tests/integration/targets/archive/tasks/remove.yml new file mode 100644 index 0000000000..44d2024068 --- /dev/null +++ b/tests/integration/targets/archive/tasks/remove.yml @@ -0,0 +1,123 @@ +--- +- name: archive using gz and remove src files + archive: + path: "{{ output_dir }}/*.txt" + dest: "{{ output_dir }}/archive_remove_01.gz" + format: gz + remove: yes + register: archive_remove_result_01 + +- debug: msg="{{ archive_remove_result_01 }}" + +- name: verify that the files archived + file: path={{ output_dir }}/archive_remove_01.gz state=file + +- name: check if gz file exists and includes all text files and src files has been removed + assert: + that: + - "{{ archive_remove_result_01.changed }}" + - "{{ 'archived' in archive_remove_result_01 }}" + - "{{ archive_remove_result_01['archived'] | length }} == 3" + +- name: remove our gz + file: path="{{ output_dir }}/archive_remove_01.gz" state=absent + +- name: check if src files has been removed + assert: + that: + - "'{{ output_dir }}/{{ item }}' is not exists" + with_items: + - foo.txt + - bar.txt + - empty.txt + +- name: prep our files again + copy: src={{ item }} dest={{ output_dir }}/{{ item }} + with_items: + - foo.txt + - bar.txt + - empty.txt + +- name: create a temporary directory to be check if it will be removed + file: + path: "{{ output_dir }}/tmpdir" + state: directory + +- name: prep our files in tmpdir + copy: src={{ item }} dest={{ output_dir }}/tmpdir/{{ item }} + with_items: + - foo.txt + - bar.txt + - empty.txt + +- name: archive using gz and remove src directory + archive: + path: "{{ output_dir }}/tmpdir" + dest: "{{ output_dir }}/archive_remove_02.gz" + format: gz + remove: yes + register: archive_remove_result_02 + +- debug: msg="{{ archive_remove_result_02 }}" + +- name: verify that the files archived + file: path={{ output_dir }}/archive_remove_02.gz state=file + +- name: check if gz file exists and includes all text files + assert: + that: + - "{{ archive_remove_result_02.changed }}" + - "{{ 'archived' in archive_remove_result_02 }}" + - "{{ archive_remove_result_02['archived'] | length }} == 3" + +- name: remove our gz + file: path="{{ output_dir }}/archive_remove_02.gz" state=absent + +- name: check if src folder has been removed + assert: + that: + - "'{{ output_dir }}/tmpdir' is not exists" + +- name: create temporary directory again + file: + path: "{{ output_dir }}/tmpdir" + state: directory + +- name: prep our files in tmpdir again + copy: src={{ item }} dest={{ output_dir }}/tmpdir/{{ item }} + with_items: + - foo.txt + - bar.txt + - empty.txt + +- name: archive using gz and remove src directory excluding one file + archive: + path: "{{ output_dir }}/tmpdir/*" + dest: "{{ output_dir }}/archive_remove_03.gz" + format: gz + remove: yes + exclude_path: "{{ output_dir }}/tmpdir/empty.txt" + register: archive_remove_result_03 + +- debug: msg="{{ archive_remove_result_03 }}" + +- name: verify that the files archived + file: path={{ output_dir }}/archive_remove_03.gz state=file + +- name: check if gz file exists and includes all text files + assert: + that: + - "{{ archive_remove_result_03.changed }}" + - "{{ 'archived' in archive_remove_result_03 }}" + - "{{ archive_remove_result_03['archived'] | length }} == 2" + +- name: remove our gz + file: path="{{ output_dir }}/archive_remove_03.gz" state=absent + +- name: verify that excluded file is still present + file: path={{ output_dir }}/tmpdir/empty.txt state=file + +- name: remove temporary directory + file: + path: "{{ output_dir }}/tmpdir" + state: absent