From 031f5fb6103a3a0313048b51af2ec7e12f48c28c Mon Sep 17 00:00:00 2001
From: John Fulton <fulton@redhat.com>
Date: Mon, 8 Apr 2019 12:25:33 -0400
Subject: [PATCH] Do not archive ceph-ansible fetch directory if it is empty

There are conditions, e.g. external ceph deployment, where
ceph-ansible will not produce a fetch directory so we should
not always assume there is something to archive.

Add a task to register the contents of the ceph-ansible fetch
directory and only archive that directory if the contents exist.

Also, remove the '--no-overwrite-dir' option from tar as it
seems to have broken repeat deployments.

Change-Id: I56eae30d73bf5cc6b1ae413c6841f86c2a4e86b5
Closes-Bug: #1823759
Closes-Bug: #1823229
---
 deployment/ceph-ansible/ceph-base.yaml | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/deployment/ceph-ansible/ceph-base.yaml b/deployment/ceph-ansible/ceph-base.yaml
index f552c65db8..6f992feaff 100644
--- a/deployment/ceph-ansible/ceph-base.yaml
+++ b/deployment/ceph-ansible/ceph-base.yaml
@@ -532,7 +532,7 @@ outputs:
                 ignore_errors: True
               - name: untar local backup of ceph-ansible fetch directory
                 # unarchive module hit https://github.com/ansible/ansible/issues/35645
-                shell: "/usr/bin/gtar --gzip --extract --no-overwrite-dir --file {{local_ceph_ansible_fetch_directory_backup}}/{{ceph_ansible_tarball_name}} -C {{playbook_dir}}/ceph-ansible/fetch_dir"
+                shell: "/usr/bin/gtar --gzip --extract --file {{local_ceph_ansible_fetch_directory_backup}}/{{ceph_ansible_tarball_name}} -C {{playbook_dir}}/ceph-ansible/fetch_dir"
                 when: local_backup_file.stat.exists == True
               when: local_ceph_ansible_fetch_directory_backup != ""
             - block: # swift backup
@@ -555,7 +555,7 @@ outputs:
                   - curl_get_http_status.stdout != "404" # new deployment
               - name: unpack downloaded ceph-ansible fetch tarball to fetch directory
                 # unarchive module hit https://github.com/ansible/ansible/issues/35645
-                shell: "/usr/bin/gtar --gzip --extract --no-overwrite-dir --file /tmp/{{old_ceph_ansible_tarball_name}} -C {{playbook_dir}}/ceph-ansible/fetch_dir"
+                shell: "/usr/bin/gtar --gzip --extract --file /tmp/{{old_ceph_ansible_tarball_name}} -C {{playbook_dir}}/ceph-ansible/fetch_dir"
                 when:
                   - curl_get_http_status.changed
                   - curl_get_http_status.stdout == "200"
@@ -619,6 +619,11 @@ outputs:
               when:
                 - outputs.changed
                 - outputs.rc != 0
+            - name: register contents of fetch_directory after ceph-ansible run
+              find:
+                paths: "{{playbook_dir}}/ceph-ansible/fetch_dir/"
+                recurse: true
+              register: ceph_ansible_fetch_dir_contents
             - name: create ceph-ansible fetch directory tarball in local backup
               archive:
                 path: "{{playbook_dir}}/ceph-ansible/fetch_dir/*"
@@ -626,7 +631,9 @@ outputs:
                 exclude_path:
                     - '{{playbook_dir}}/ceph-ansible/fetch_dir/fetch_dir'
               register: nested
-              when: local_ceph_ansible_fetch_directory_backup != ""
+              when:
+                - local_ceph_ansible_fetch_directory_backup != ""
+                - ceph_ansible_fetch_dir_contents.matched|int != 0
             - block: # swift backup
               - name: create temporary ceph-ansible fetch directory tarball for swift backup
                 archive:
@@ -651,7 +658,9 @@ outputs:
                   state: absent
                 when: (curl_put_http_status.changed and (curl_put_http_status.stdout == "200" or
                        curl_put_http_status.stdout == "201"))
-              when: local_ceph_ansible_fetch_directory_backup == ""
+              when:
+                - local_ceph_ansible_fetch_directory_backup == ""
+                - ceph_ansible_fetch_dir_contents.matched|int != 0
             - name: remove nested broken path if exist
               file:
                 state: absent