Skip to content
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
33 changes: 25 additions & 8 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ def find_modules(root_dir) -> dict:
return entries


def write_to_console(conpath, text):
with open(conpath, "w") as wfh:
wfh.write(text)
wfh.flush()


def multi_log(
text,
console=True,
Expand All @@ -393,14 +399,25 @@ def multi_log(
sys.stderr.write(text)
if console:
conpath = "/dev/console"
writing_to_console_worked = False
if os.path.exists(conpath):
with open(conpath, "w") as wfh:
wfh.write(text)
wfh.flush()
elif fallback_to_stdout:
# A container may lack /dev/console (arguably a container bug). If
# it does not exist, then write output to stdout. this will result
# in duplicate stderr and stdout messages if stderr was True.
try:
write_to_console(conpath, text)
writing_to_console_worked = True
except OSError:
Comment thread
andrew-lee-1089 marked this conversation as resolved.
console_error = "Failed to write to /dev/console"
sys.stdout.write(f"{console_error}\n")
if log:
log.log(logging.WARNING, console_error)

if fallback_to_stdout and not writing_to_console_worked:
# A container may lack /dev/console (arguably a container bug).
# Additionally, /dev/console may not be writable to on a VM (again
# likely a VM bug or virtualization bug).
#
# If either of these is the case, then write output to stdout.
# This will result in duplicate stderr and stdout messages if
# stderr was True.
#
# even though upstart or systemd might have set up output to go to
# /dev/console, the user may have configured elsewhere via
Expand Down Expand Up @@ -2061,7 +2078,7 @@ def write_file(
omode="wb",
preserve_mode=False,
*,
ensure_dir_exists=True
ensure_dir_exists=True,
):
"""
Writes a file with the given content and sets the file mode as specified.
Expand Down
27 changes: 27 additions & 0 deletions tests/unittests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,33 @@ def test_logs_dont_go_to_stdout_if_fallback_to_stdout_is_false(self):
util.multi_log("something", fallback_to_stdout=False)
self.assertEqual("", self.stdout.getvalue())

@mock.patch(
"cloudinit.util.write_to_console",
mock.Mock(side_effect=OSError("Failed to write to console")),
)
def test_logs_go_to_stdout_if_writing_to_console_fails_and_fallback_true(
self,
):
self._createConsole(self.root)
util.multi_log("something", fallback_to_stdout=True)
self.assertEqual(
"Failed to write to /dev/console\nsomething",
self.stdout.getvalue(),
)

@mock.patch(
"cloudinit.util.write_to_console",
mock.Mock(side_effect=OSError("Failed to write to console")),
)
def test_logs_go_nowhere_if_writing_to_console_fails_and_fallback_false(
self,
):
self._createConsole(self.root)
util.multi_log("something", fallback_to_stdout=False)
self.assertEqual(
"Failed to write to /dev/console\n", self.stdout.getvalue()
)

def test_logs_go_to_log_if_given(self):
log = mock.MagicMock()
logged_string = "something very important"
Expand Down
1 change: 1 addition & 0 deletions tools/.github-cla-signers
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Aman306
andgein
andrewbogott
andrewlukoshko
andrew-lee-metaswitch
antonyc
aswinrajamannar
beantaxi
Expand Down