Skip to content

Commit

Permalink
Do not set up logger multiple times
Browse files Browse the repository at this point in the history
Add a new attribute flag to the argparser Namespace attribute which is used
to disable logging.

This isn't elegant, but fixing logging is going to be a large refactor
so this gets logging "working" for now while minimizing number of LOC
changed
  • Loading branch information
holmanb committed Jul 22, 2024
1 parent 19f9910 commit 1a53861
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
43 changes: 31 additions & 12 deletions cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,11 @@ def main_init(name, args):
outfmt = None
errfmt = None
try:
close_stdin(lambda msg: early_logs.append((logging.DEBUG, msg)))
outfmt, errfmt = util.fixup_output(init.cfg, name)
if not args.skip_log_setup:
close_stdin(lambda msg: early_logs.append((logging.DEBUG, msg)))
outfmt, errfmt = util.fixup_output(init.cfg, name)
else:
outfmt, errfmt = util.get_output_cfg(init.cfg, name)
except Exception:
msg = "Failed to setup output redirection!"
util.logexc(LOG, msg)
Expand All @@ -363,8 +366,9 @@ def main_init(name, args):
"Logging being reset, this logger may no longer be active shortly"
)
log.reset_logging()
log.setup_logging(init.cfg)
apply_reporting_cfg(init.cfg)
if not args.skip_log_setup:
log.setup_logging(init.cfg)
apply_reporting_cfg(init.cfg)

# Any log usage prior to setup_logging above did not have local user log
# config applied. We send the welcome message now, as stderr/out have
Expand Down Expand Up @@ -619,8 +623,9 @@ def main_modules(action_name, args):
mods = Modules(init, extract_fns(args), reporter=args.reporter)
# Stage 4
try:
close_stdin()
util.fixup_output(mods.cfg, name)
if not args.skip_log_setup:
close_stdin()
util.fixup_output(mods.cfg, name)
except Exception:
util.logexc(LOG, "Failed to setup output redirection!")
if args.debug:
Expand All @@ -629,8 +634,9 @@ def main_modules(action_name, args):
"Logging being reset, this logger may no longer be active shortly"
)
log.reset_logging()
log.setup_logging(mods.cfg)
apply_reporting_cfg(init.cfg)
if not args.skip_log_setup:
log.setup_logging(mods.cfg)
apply_reporting_cfg(init.cfg)

# now that logging is setup and stdout redirected, send welcome
welcome(name, msg=w_msg)
Expand Down Expand Up @@ -1142,6 +1148,7 @@ def main(sysv_args=None):
parser.error("a subcommand is required")

args = parser.parse_args(args=sysv_args)
setattr(args, "skip_log_setup", False)
if not args.single_process:
return sub_main(args)
LOG.info("Running cloud-init in single process mode.")
Expand All @@ -1154,23 +1161,35 @@ def main(sysv_args=None):
socket.sd_notify("READY=1")
# wait for cloud-init-local.service to start
with sync("local"):
# set up logger
args = parser.parse_args(args=["init", "--local"])
args.skip_log_setup = False
# run local stage
sub_main(parser.parse_args(args=["init", "--local"]))
sub_main(args)

# wait for cloud-init.service to start
with sync("network"):
# skip re-setting up logger
args = parser.parse_args(args=["init"])
args.skip_log_setup = True
# run init stage
sub_main(parser.parse_args(args=["init"]))
sub_main(args)

# wait for cloud-config.service to start
with sync("config"):
# skip re-setting up logger
args = parser.parse_args(args=["modules", "--mode=config"])
args.skip_log_setup = True
# run config stage
sub_main(parser.parse_args(args=["modules", "--mode=config"]))
sub_main(args)

# wait for cloud-final.service to start
with sync("final"):
# skip re-setting up logger
args = parser.parse_args(args=["modules", "--mode=final"])
args.skip_log_setup = True
# run final stage
sub_main(parser.parse_args(args=["modules", "--mode=final"]))
sub_main(args)

# signal completion to cloud-init.service
if sync.first_exception:
Expand Down
6 changes: 5 additions & 1 deletion tests/unittests/cmd/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from cloudinit.util import ensure_dir, load_text_file, write_file
from tests.unittests.helpers import FilesystemMockingTestCase, wrap_and_call

MyArgs = namedtuple("MyArgs", "debug files force local reporter subcommand")
MyArgs = namedtuple(
"MyArgs", "debug files force local reporter subcommand skip_log_setup"
)


class TestMain(FilesystemMockingTestCase):
Expand Down Expand Up @@ -76,6 +78,7 @@ def test_main_init_run_net_runs_modules(self):
local=False,
reporter=None,
subcommand="init",
skip_log_setup=False,
)
(_item1, item2) = wrap_and_call(
"cloudinit.cmd.main",
Expand Down Expand Up @@ -122,6 +125,7 @@ def test_main_init_run_net_calls_set_hostname_when_metadata_present(self):
local=False,
reporter=None,
subcommand="init",
skip_log_setup=False,
)

def set_hostname(name, cfg, cloud, args):
Expand Down

0 comments on commit 1a53861

Please sign in to comment.