Skip to content

Commit

Permalink
fix: wait for cloud-init.service to fully activate
Browse files Browse the repository at this point in the history
Once the systemd constraint on After cloud-config.service was
removed, there are situations where cloud-config.service runs so
long after pro that the python code that waits for
cloud-config.service to finish activating is being bypassed as if
cloud-init isn't on the system.  This change forces pro to wait
for cloud-config to finish if cloud-init is running at all.
  • Loading branch information
catmsred committed Mar 27, 2024
1 parent 9e9df67 commit a169227
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
13 changes: 12 additions & 1 deletion lib/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ def _wait_for_cloud_config():
LOG.debug("waiting for cloud-config.service to finish")
for i in range(WAIT_FOR_CLOUD_CONFIG_POLL_TIMES + 1):
state = system.get_systemd_unit_active_state("cloud-config.service")
ci_state = system.get_systemd_unit_active_state("cloud-init.service")
LOG.debug("cloud-config.service state: %r", state)
if state is not None and state == "activating":
LOG.debug("cloud-init.service state: %r", ci_state)
# if cloud-config.service is not yet activating but cloud-init is
# running, wait for cloud-config to start
if state is not None and (
state == "activating"
or (
state == "inactive"
and ci_state is not None
and (ci_state == "activating" or ci_state == "active")
)
):
if i < WAIT_FOR_CLOUD_CONFIG_POLL_TIMES:
LOG.debug(
"cloud-config.service is activating. "
Expand Down
30 changes: 24 additions & 6 deletions uaclient/tests/test_lib_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,41 @@ class TestWaitForCloudConfig:
(
# not activating
(
["active"],
["active"] * 2,
[],
),
# inactive (all cloud-init)
(
["inactive"],
["inactive"] * 2,
[],
),
(
[None],
[None] * 2,
[],
),
# activating, then finishes
# cloud-config activating, then finishes
# cloud-init is active
(
(["activating"] * 11) + ["active"],
(["activating", "active"] * 11) + ["active"] * 2,
[mock.call(WAIT_FOR_CLOUD_CONFIG_SLEEP_TIME)] * 11,
),
(
(["activating"] * 11) + ["failed"],
(["activating", "active"] * 11) + ["failed"] + ["active"],
[mock.call(WAIT_FOR_CLOUD_CONFIG_SLEEP_TIME)] * 11,
),
# inactive cloud-config, active cloud-init
(
(["inactive", "active"] * 6)
+ (["activating", "active"] * 5)
+ ["active"] * 2,
[mock.call(WAIT_FOR_CLOUD_CONFIG_SLEEP_TIME)] * 11,
),
# inactive cloud-config, activating cloud-init
(
(["inactive", "activating"] * 2)
+ (["inactive", "active"] * 4)
+ (["activating", "active"] * 5)
+ ["active"] * 2,
[mock.call(WAIT_FOR_CLOUD_CONFIG_SLEEP_TIME)] * 11,
),
# still activating after polling maximum times
Expand All @@ -54,6 +71,7 @@ def test_wait_for_cloud_config(
active_state_side_effect,
expected_sleep_calls,
):
print(active_state_side_effect)
m_get_systemd_unit_active_state.side_effect = active_state_side_effect
_wait_for_cloud_config()
assert m_sleep.call_args_list == expected_sleep_calls

0 comments on commit a169227

Please sign in to comment.