summaryrefslogtreecommitdiff
path: root/ironic_python_agent
diff options
context:
space:
mode:
authorJulia Kreger <juliaashleykreger@gmail.com>2021-03-30 07:58:34 -0700
committerJulia Kreger <juliaashleykreger@gmail.com>2021-04-01 11:16:20 -0700
commitdf418984f037856813b6bc5e495ef602a6737ee3 (patch)
treedaa6b60b295567538e3a99ecb4440ebf531c4170 /ironic_python_agent
parent49d123dd6e32ea1455da1a2f11d6e624d435c308 (diff)
downloadironic-python-agent-df418984f037856813b6bc5e495ef602a6737ee3.tar.gz
Capture the early logging
_early_log prints to stdout, which is fine in some cases, however in other cases it gets lost in the shuffle of process launch by things like systemd. Lets try to save everything, and re-log it so it is easy to debug early issues. Change-Id: I334a9073d17cccec4c669fae82edc3e388debc5c
Diffstat (limited to 'ironic_python_agent')
-rw-r--r--ironic_python_agent/cmd/agent.py2
-rw-r--r--ironic_python_agent/tests/unit/test_utils.py23
-rw-r--r--ironic_python_agent/utils.py13
3 files changed, 36 insertions, 2 deletions
diff --git a/ironic_python_agent/cmd/agent.py b/ironic_python_agent/cmd/agent.py
index 92530347..9ebe3006 100644
--- a/ironic_python_agent/cmd/agent.py
+++ b/ironic_python_agent/cmd/agent.py
@@ -46,7 +46,7 @@ def run():
logger = log.getLogger(__name__)
logger.debug("Configuration:")
CONF.log_opt_values(logger, log.DEBUG)
-
+ utils.log_early_log_to_logger()
agent.IronicPythonAgent(CONF.api_url,
agent.Host(hostname=CONF.advertise_host,
port=CONF.advertise_port),
diff --git a/ironic_python_agent/tests/unit/test_utils.py b/ironic_python_agent/tests/unit/test_utils.py
index 02f2f95d..640d50ca 100644
--- a/ironic_python_agent/tests/unit/test_utils.py
+++ b/ironic_python_agent/tests/unit/test_utils.py
@@ -1195,3 +1195,26 @@ class TestCheckVirtualMedia(ironic_agent_base.IronicAgentTest):
mock_execute.assert_called_with('lsblk', '-n', '-s', '-P', '-b',
'-oKNAME,TRAN,TYPE,SIZE',
'/dev/sdh')
+
+
+class TestCheckEarlyLogging(ironic_agent_base.IronicAgentTest):
+
+ @mock.patch.object(utils, 'LOG', autospec=True)
+ def test_early_logging_goes_to_logger(self, mock_log):
+ info = mock.Mock()
+ mock_log.info.side_effect = info
+ # Reset the buffer to be empty.
+ utils._EARLY_LOG_BUFFER = []
+ # Store some data via _early_log
+ utils._early_log('line 1.')
+ utils._early_log('line 2 %s', 'message')
+ expected_messages = ['line 1.',
+ 'line 2 message']
+ self.assertEqual(expected_messages, utils._EARLY_LOG_BUFFER)
+ # Test we've got data in the buffer.
+ info.assert_not_called()
+ # Test the other half of this.
+ utils.log_early_log_to_logger()
+ expected_calls = [mock.call('Early logging: %s', 'line 1.'),
+ mock.call('Early logging: %s', 'line 2 message')]
+ info.assert_has_calls(expected_calls)
diff --git a/ironic_python_agent/utils.py b/ironic_python_agent/utils.py
index f045a4fe..dd68a526 100644
--- a/ironic_python_agent/utils.py
+++ b/ironic_python_agent/utils.py
@@ -78,6 +78,8 @@ DEVICE_EXTRACTOR = re.compile(r'^(?:(.*\d)p|(.*\D))(?:\d+)$')
PARTED_TABLE_TYPE_REGEX = re.compile(r'^.*partition\s+table\s*:\s*(gpt|msdos)',
re.IGNORECASE)
+_EARLY_LOG_BUFFER = []
+
def execute(*cmd, **kwargs):
"""Convenience wrapper around ironic_lib's execute() method.
@@ -187,7 +189,16 @@ def _get_vmedia_params():
def _early_log(msg, *args):
"""Log via printing (before oslo.log is configured)."""
- print('ironic-python-agent:', msg % args, file=sys.stderr)
+ log_entry = msg % args
+ _EARLY_LOG_BUFFER.append(log_entry)
+ print('ironic-python-agent:', log_entry, file=sys.stderr)
+
+
+def log_early_log_to_logger():
+ """Logs early logging events to the configured logger."""
+
+ for entry in _EARLY_LOG_BUFFER:
+ LOG.info("Early logging: %s", entry)
def _copy_config_from(path):