Skip to content

Commit

Permalink
Ignore hotplug socket when collecting logs (#985)
Browse files Browse the repository at this point in the history
Update "cloud-init collect-logs" to ignore
/run/cloud-init/hook-hotplug-cmd as this will raise the error
"/run/cloud-init/hook-hotplug-cmd` is a named pipe" if included.

Also updated logs.py to continue writing the tarball if it fails
collecting a file rather than let the exception bubble up.

LP: #1940235
  • Loading branch information
TheRealFalcon authored Aug 19, 2021
1 parent 3e63025 commit 776bd36
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cloudinit/cmd/devel/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ def get_parser(parser=None):
return parser


def _copytree_ignore_sensitive_files(curdir, files):
"""Return a list of files to ignore if we are non-root"""
if os.getuid() == 0:
return ()
return (INSTANCE_JSON_SENSITIVE_FILE,) # Ignore root-permissioned files
def _copytree_rundir_ignore_files(curdir, files):
"""Return a list of files to ignore for /run/cloud-init directory"""
ignored_files = [
'hook-hotplug-cmd', # named pipe for hotplug
]
if os.getuid() != 0:
# Ignore root-permissioned files
ignored_files.append(INSTANCE_JSON_SENSITIVE_FILE)
return ignored_files


def _write_command_output_to_file(cmd, filename, msg, verbosity):
Expand Down Expand Up @@ -123,9 +127,13 @@ def collect_logs(tarfile, include_userdata, verbosity=0):
run_dir = os.path.join(log_dir, 'run')
ensure_dir(run_dir)
if os.path.exists(CLOUDINIT_RUN_DIR):
shutil.copytree(CLOUDINIT_RUN_DIR,
os.path.join(run_dir, 'cloud-init'),
ignore=_copytree_ignore_sensitive_files)
try:
shutil.copytree(CLOUDINIT_RUN_DIR,
os.path.join(run_dir, 'cloud-init'),
ignore=_copytree_rundir_ignore_files)
except shutil.Error as e:
sys.stderr.write("Failed collecting file(s) due to error:\n")
sys.stderr.write(str(e) + '\n')
_debug("collected dir %s\n" % CLOUDINIT_RUN_DIR, 1, verbosity)
else:
_debug("directory '%s' did not exist\n" % CLOUDINIT_RUN_DIR, 1,
Expand Down

0 comments on commit 776bd36

Please sign in to comment.