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

Backport/2.7/48148: net_put module leaves empty files behind #48151

Merged
merged 2 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/48148_net_put_cleanup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "net_put - fix when net_put module leaves temp files in some network OS cases e.g. routerOS"
8 changes: 7 additions & 1 deletion lib/ansible/plugins/action/net_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import uuid
import hashlib
import sys
import re

from ansible.module_utils._text import to_text, to_bytes
from ansible.module_utils.connection import Connection
Expand Down Expand Up @@ -148,7 +149,11 @@ def _handle_existing_file(self, conn, source, dest, proto, timeout):
proto=proto, timeout=timeout
)
except Exception as exc:
if (to_text(exc)).find("No such file or directory") > 0:
pattern = to_text(exc)
not_found_exc = "No such file or directory"
if re.search(not_found_exc, pattern, re.I):
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this is slower than the old code. The standard way to do it would have been:

if u'no such file or directory' in to_text(exc).lower():
    if os.path.exists(source_file):
        os.remove(source_file)
    return True

if os.path.exists(source_file):
os.remove(source_file)
return True
else:
try:
Expand All @@ -162,6 +167,7 @@ def _handle_existing_file(self, conn, source, dest, proto, timeout):
with open(source_file, 'r') as f:
old_content = f.read()
except (IOError, OSError) as ioexc:
os.remove(source_file)
raise IOError(ioexc)

sha1 = hashlib.sha1()
Expand Down