Skip to content

Remove temp files created by submit_debug_info #2714

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

Merged
merged 2 commits into from
May 27, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions st2debug/st2debug/cmd/submit_debug_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from st2debug.utils.fs import get_full_file_list
from st2debug.utils.fs import get_dirs_in_path
from st2debug.utils.fs import remove_file
from st2debug.utils.fs import remove_dir
from st2debug.utils.system_info import get_cpu_info
from st2debug.utils.system_info import get_memory_info
from st2debug.utils.system_info import get_package_list
Expand Down Expand Up @@ -240,12 +241,12 @@ def create_archive(self):
try:
# 1. Create temporary directory with the final directory structure where we will move
# files which will be processed and included in the tarball
temp_dir_path = self.create_temp_directories()
self._temp_dir_path = self.create_temp_directories()

# Prepend temp_dir_path to OUTPUT_PATHS
output_paths = {}
for key, path in OUTPUT_PATHS.iteritems():
output_paths[key] = os.path.join(temp_dir_path, path)
output_paths[key] = os.path.join(self._temp_dir_path, path)

# 2. Moves all the files to the temporary directory
LOG.info('Collecting files...')
Expand All @@ -263,12 +264,16 @@ def create_archive(self):
self.add_shell_command_output(output_paths['commands'])

# 3. Create a tarball
return self.create_tarball(temp_dir_path)
return self.create_tarball(self._temp_dir_path)

except Exception as e:
LOG.exception('Failed to generate tarball', exc_info=True)
raise e

finally:
# Ensure temp files are removed regardless of success or failure
remove_dir(self._temp_dir_path)

def encrypt_archive(self, archive_file_path):
"""
Encrypt archive with debugging information using our public key.
Expand Down
17 changes: 17 additions & 0 deletions st2debug/st2debug/utils/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ def remove_file(file_path, ignore_errors=True):
except Exception as e:
if not ignore_errors:
raise e


def remove_dir(dir_path, ignore_errors=True):
"""
Recursively remove a directory.

:param dir_path: Directory to be removed.
:type dir_path: ``str``

:param ignore_errors: True to ignore errors during removal.
:type ignore_errors: ``bool``
"""
try:
shutil.rmtree(dir_path, ignore_errors)
except Exception as e:
if not ignore_errors:
raise e
10 changes: 10 additions & 0 deletions st2debug/tests/integration/test_submit_debug_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ def test_create_archive_include_all(self):
extract_path=extract_path,
required_directories=['logs', 'configs', 'content'])

def test_create_archive_deletes_temp_dir(self):
debug_collector = DebugInfoCollector(include_logs=True, include_configs=True,
include_content=True,
include_system_info=True)
archive_path = debug_collector.create_archive()
self.to_delete_files.append(archive_path)

self.assertTrue(debug_collector._temp_dir_path)
self.assertTrue(not os.path.exists(debug_collector._temp_dir_path))

def _get_yaml_config(self):
return {
'log_file_paths': [
Expand Down