Skip to content

Commit

Permalink
refactor: Refactor status.py (#4864)
Browse files Browse the repository at this point in the history
The one major functional change in this commit is around how we detect
running vs error states. Status reporting has a fundamental problem in
that we can't accurately tell if cloud-init is done because cloud-init
is actually several processes. There isn't always a way to tell whether
a service isn't running because it simply hasn't started yet vs the
service being blocked/crashed and will never start/finish.

In the past, if any of the cloud-init services reported an error, we
would assume that cloud-init as a whole has crashed and report that
cloud-init is "done", but with error. This commit flips that logic to
assume that cloud-init is always running unless we see indication that
cloud-init has completely finished. This means that
`cloud-init status --wait` may run forever if cloud-init has crashed or
is blocked on another service. This is preferable to returning early
and potentially allowing provisioning scripts that wait for cloud-init
to continue provisioning. On systemd-enabled systems, there is extra
logic to inspect the state of the services, so this should rarely be a
problem in practice.

Additionally, this commit includes the following refactoring:
- Split UXAppStatus into RunningStatus and ConditionStatus so they can
  be tracked independently
- Simplify the tabular printing
- On error print extended_status as "error - done" or "error - running"
- Add several helper functions in `get_status_details` to simplify
  logic
- Rename `_get_error_or_running_from_systemd` to `systemd_failed`
  and only return if error is detected
- Change "is running" logic to be determined solely by the existence
  of the status.json and results.json files.
  • Loading branch information
TheRealFalcon authored and blackboxsw committed Feb 27, 2024
1 parent 3817a5c commit 5ad320c
Show file tree
Hide file tree
Showing 6 changed files with 481 additions and 392 deletions.
10 changes: 5 additions & 5 deletions cloudinit/cmd/cloud_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import sys

from cloudinit.cmd.devel import read_cfg_paths
from cloudinit.cmd.status import UXAppStatus, get_status_details
from cloudinit.cmd.status import RunningStatus, get_status_details
from cloudinit.sources import METADATA_UNKNOWN, canonical_cloud_id
from cloudinit.util import error

Expand Down Expand Up @@ -66,11 +66,11 @@ def handle_args(name, args):
@return: 0 on success, 1 on error, 2 on disabled, 3 on cloud-init not run.
"""
status_details = get_status_details()
if status_details.status == UXAppStatus.DISABLED:
sys.stdout.write("{0}\n".format(status_details.status.value))
if status_details.running_status == RunningStatus.DISABLED:
sys.stdout.write("{0}\n".format(status_details.running_status.value))
return 2
elif status_details.status == UXAppStatus.NOT_RUN:
sys.stdout.write("{0}\n".format(status_details.status.value))
elif status_details.running_status == RunningStatus.NOT_STARTED:
sys.stdout.write("{0}\n".format(status_details.running_status.value))
return 3

try:
Expand Down
Loading

0 comments on commit 5ad320c

Please sign in to comment.