diff options
author | Alberto Contreras <alberto.contreras@canonical.com> | 2022-11-23 21:54:33 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-23 21:54:33 +0100 |
commit | 4ba809b5fe2dca02999459c09b7db3ebb1731f59 (patch) | |
tree | d3aed035ff530dff0688d8de311bf754acf9976d /cloudinit/cmd | |
parent | 65eb520debc1ce804be05d1191389fe987726aaf (diff) | |
download | cloud-init-git-4ba809b5fe2dca02999459c09b7db3ebb1731f59.tar.gz |
status: handle ds not defined in status.json (#1876)
Handles any situation where `status.json` does not yet contain
datasource information, by gracefully fulfilling a
`datasource=None` in `StatusDetails`.
LP: #1997559
Diffstat (limited to 'cloudinit/cmd')
-rw-r--r-- | cloudinit/cmd/status.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/cloudinit/cmd/status.py b/cloudinit/cmd/status.py index df136288..e1c37a78 100644 --- a/cloudinit/cmd/status.py +++ b/cloudinit/cmd/status.py @@ -13,11 +13,12 @@ import json import os import sys from time import gmtime, sleep, strftime -from typing import Any, Dict, List, NamedTuple, Tuple, Union +from typing import Any, Dict, List, NamedTuple, Optional, Tuple, Union from cloudinit import safeyaml from cloudinit.cmd.devel import read_cfg_paths from cloudinit.distros import uses_systemd +from cloudinit.helpers import Paths from cloudinit.util import get_cmdline, load_file, load_json CLOUDINIT_DISABLED_FILE = "/etc/cloud/cloud-init.disabled" @@ -63,7 +64,7 @@ class StatusDetails(NamedTuple): description: str errors: List[str] last_update: str - datasource: str + datasource: Optional[str] TABULAR_LONG_TMPL = """\ @@ -124,7 +125,7 @@ def handle_status_args(name, args) -> int: sys.stdout.flush() details = get_status_details(paths) sleep(0.25) - details_dict: Dict[str, Union[str, List[str], Dict[str, Any]]] = { + details_dict: Dict[str, Union[None, str, List[str], Dict[str, Any]]] = { "datasource": details.datasource, "boot_status_code": details.boot_status_code.value, "status": details.status.value, @@ -195,7 +196,7 @@ def get_bootstatus(disable_file, paths) -> Tuple[UXAppBootStatusCode, str]: return (bootstatus_code, reason) -def get_status_details(paths=None) -> StatusDetails: +def get_status_details(paths: Optional[Paths] = None) -> StatusDetails: """Return a dict with status, details and errors. @param paths: An initialized cloudinit.helpers.paths object. @@ -206,7 +207,7 @@ def get_status_details(paths=None) -> StatusDetails: status = UXAppStatus.NOT_RUN errors = [] - datasource = "" + datasource: Optional[str] = "" status_v1 = {} status_file = os.path.join(paths.run_dir, "status.json") @@ -228,9 +229,14 @@ def get_status_details(paths=None) -> StatusDetails: status = UXAppStatus.RUNNING description = "Running in stage: {0}".format(value) elif key == "datasource": + if value is None: + # If ds not yet written in status.json, then keep previous + # description + datasource = value + continue description = value - datasource, _, _ = value.partition(" ") - datasource = datasource.lower().replace("datasource", "") + ds, _, _ = value.partition(" ") + datasource = ds.lower().replace("datasource", "") elif isinstance(value, dict): errors.extend(value.get("errors", [])) start = value.get("start") or 0 |