diff options
author | James Falcon <james.falcon@canonical.com> | 2022-09-02 13:34:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-02 12:34:26 -0600 |
commit | 5c15e24f7cb781db85fa31720d48988bf59db31d (patch) | |
tree | b8896ecab46e405bfea9462c79d775154be29fa8 /cloudinit/cmd | |
parent | 3eb2dfce6046fbe67bab7475b8ed17c1d38d64e5 (diff) | |
download | cloud-init-git-5c15e24f7cb781db85fa31720d48988bf59db31d.tar.gz |
Refactor instance json files to use Paths (SC-1238) (#1709)
We have multiple areas of the codebase where we fetch the rundir path
and join it with the filename for instance-data.json or
instance-data-sensitive.json. This commit refactors those calls to use a
'Paths' lookup instead.
Diffstat (limited to 'cloudinit/cmd')
-rwxr-xr-x | cloudinit/cmd/cloud_id.py | 18 | ||||
-rwxr-xr-x | cloudinit/cmd/devel/logs.py | 4 | ||||
-rwxr-xr-x | cloudinit/cmd/devel/render.py | 7 | ||||
-rwxr-xr-x | cloudinit/cmd/main.py | 8 | ||||
-rwxr-xr-x | cloudinit/cmd/query.py | 21 |
5 files changed, 23 insertions, 35 deletions
diff --git a/cloudinit/cmd/cloud_id.py b/cloudinit/cmd/cloud_id.py index 567d341a..e2e69bef 100755 --- a/cloudinit/cmd/cloud_id.py +++ b/cloudinit/cmd/cloud_id.py @@ -8,16 +8,11 @@ import argparse import json import sys +from cloudinit.cmd.devel import read_cfg_paths from cloudinit.cmd.status import UXAppStatus, get_status_details -from cloudinit.sources import ( - INSTANCE_JSON_FILE, - METADATA_UNKNOWN, - canonical_cloud_id, -) +from cloudinit.sources import METADATA_UNKNOWN, canonical_cloud_id from cloudinit.util import error -DEFAULT_INSTANCE_JSON = "/run/cloud-init/%s" % INSTANCE_JSON_FILE - NAME = "cloud-id" @@ -30,6 +25,7 @@ def get_parser(parser=None): @returns: ArgumentParser with proper argument configuration. """ + default_instance_json = read_cfg_paths().get_runpath("instance_data") if not parser: parser = argparse.ArgumentParser( prog=NAME, @@ -53,9 +49,11 @@ def get_parser(parser=None): "-i", "--instance-data", type=str, - default=DEFAULT_INSTANCE_JSON, - help="Path to instance-data.json file. Default is %s" - % DEFAULT_INSTANCE_JSON, + default=default_instance_json, + help=( + "Path to instance-data.json file. " + f"Default is {default_instance_json}" + ), ) return parser diff --git a/cloudinit/cmd/devel/logs.py b/cloudinit/cmd/devel/logs.py index a87b7043..69f4d931 100755 --- a/cloudinit/cmd/devel/logs.py +++ b/cloudinit/cmd/devel/logs.py @@ -13,7 +13,7 @@ import sys from datetime import datetime from cloudinit.cmd.devel import read_cfg_paths -from cloudinit.sources import INSTANCE_JSON_SENSITIVE_FILE +from cloudinit.helpers import Paths from cloudinit.subp import ProcessExecutionError, subp from cloudinit.temp_utils import tempdir from cloudinit.util import chdir, copy, ensure_dir, write_file @@ -80,7 +80,7 @@ def _copytree_rundir_ignore_files(curdir, files): ] if os.getuid() != 0: # Ignore root-permissioned files - ignored_files.append(INSTANCE_JSON_SENSITIVE_FILE) + ignored_files.append(Paths({}).lookups["instance_data_sensitive"]) return ignored_files diff --git a/cloudinit/cmd/devel/render.py b/cloudinit/cmd/devel/render.py index 62b432d2..e28cc0af 100755 --- a/cloudinit/cmd/devel/render.py +++ b/cloudinit/cmd/devel/render.py @@ -10,7 +10,6 @@ import sys from cloudinit import log from cloudinit.handlers.jinja_template import render_jinja_payload_from_file -from cloudinit.sources import INSTANCE_JSON_FILE, INSTANCE_JSON_SENSITIVE_FILE from . import addLogHandlerCLI, read_cfg_paths @@ -65,11 +64,9 @@ def handle_args(name, args): else: paths = read_cfg_paths() uid = os.getuid() - redacted_data_fn = os.path.join(paths.run_dir, INSTANCE_JSON_FILE) + redacted_data_fn = paths.get_runpath("instance_data") if uid == 0: - instance_data_fn = os.path.join( - paths.run_dir, INSTANCE_JSON_SENSITIVE_FILE - ) + instance_data_fn = paths.get_runpath("instance_data_sensitive") if not os.path.exists(instance_data_fn): LOG.warning( "Missing root-readable %s. Using redacted %s instead.", diff --git a/cloudinit/cmd/main.py b/cloudinit/cmd/main.py index 6134d7c4..18f1678c 100755 --- a/cloudinit/cmd/main.py +++ b/cloudinit/cmd/main.py @@ -802,12 +802,10 @@ def status_wrapper(name, args, data_d=None, link_d=None): return len(v1[mode]["errors"]) -def _maybe_persist_instance_data(init): +def _maybe_persist_instance_data(init: stages.Init): """Write instance-data.json file if absent and datasource is restored.""" - if init.ds_restored: - instance_data_file = os.path.join( - init.paths.run_dir, sources.INSTANCE_JSON_FILE - ) + if init.datasource and init.ds_restored: + instance_data_file = init.paths.get_runpath("instance_data") if not os.path.exists(instance_data_file): init.datasource.persist_instance_data(write_cache=False) diff --git a/cloudinit/cmd/query.py b/cloudinit/cmd/query.py index 2dcd8e44..9add6a71 100755 --- a/cloudinit/cmd/query.py +++ b/cloudinit/cmd/query.py @@ -27,11 +27,7 @@ from cloudinit.handlers.jinja_template import ( get_jinja_variable_alias, render_jinja_payload, ) -from cloudinit.sources import ( - INSTANCE_JSON_FILE, - INSTANCE_JSON_SENSITIVE_FILE, - REDACT_SENSITIVE_VALUE, -) +from cloudinit.sources import REDACT_SENSITIVE_VALUE NAME = "query" LOG = log.getLogger(NAME) @@ -59,8 +55,10 @@ def get_parser(parser=None): "-i", "--instance-data", type=str, - help="Path to instance-data.json file. Default is /run/cloud-init/%s" - % INSTANCE_JSON_FILE, + help=( + "Path to instance-data.json file. Default is " + f"{read_cfg_paths().get_runpath('instance_data')}" + ), ) parser.add_argument( "-l", @@ -151,16 +149,13 @@ def _read_instance_data(instance_data, user_data, vendor_data) -> dict: access perms. """ uid = os.getuid() - if not all([instance_data, user_data, vendor_data]): - paths = read_cfg_paths() + paths = read_cfg_paths() if instance_data: instance_data_fn = instance_data else: - redacted_data_fn = os.path.join(paths.run_dir, INSTANCE_JSON_FILE) + redacted_data_fn = paths.get_runpath("instance_data") if uid == 0: - sensitive_data_fn = os.path.join( - paths.run_dir, INSTANCE_JSON_SENSITIVE_FILE - ) + sensitive_data_fn = paths.get_runpath("instance_data_sensitive") if os.path.exists(sensitive_data_fn): instance_data_fn = sensitive_data_fn else: |