summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2022-02-10 13:18:00 -0700
committerGitHub <noreply@github.com>2022-02-10 14:18:00 -0600
commit217ef6ba6c52788f4363b998b6da08863fea5cd9 (patch)
treeb6a530d4608c8d170de89616049702e29abd3af8 /cloudinit
parente559a8f8ec4047600cb85dc4151e012108a2d54d (diff)
downloadvyos-cloud-init-217ef6ba6c52788f4363b998b6da08863fea5cd9.tar.gz
vyos-cloud-init-217ef6ba6c52788f4363b998b6da08863fea5cd9.zip
cloud-id: publish /run/cloud-init/cloud-id-<cloud-type> files (#1244)
Once a valid datasource is detected, publish the following artifacts to expedite cloud-identification without having to invoke cloud-id from shell scripts or sheling out from python. These files can also be relied on in systemd ConditionPathExists directives to limit execution of services and units to specific clouds. /run/cloud-init/cloud-id: - A symlink with content that is the canonical cloud-id of the datasource detected. This content is the same lower-case value as the output of /usr/bin/cloud-id. /run/cloud-init/cloud-id-<canonical-cloud-id>: - A single file which will contain the canonical cloud-id encoded in the filename
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/sources/__init__.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py
index bd862cdd..88028cfa 100644
--- a/cloudinit/sources/__init__.py
+++ b/cloudinit/sources/__init__.py
@@ -303,6 +303,9 @@ class DataSource(CloudInitPickleMixin, metaclass=abc.ABCMeta):
"_beta_keys": ["subplatform"],
"availability-zone": availability_zone,
"availability_zone": availability_zone,
+ "cloud_id": canonical_cloud_id(
+ self.cloud_name, self.region, self.platform_type
+ ),
"cloud-name": self.cloud_name,
"cloud_name": self.cloud_name,
"distro": sysinfo["dist"][0],
@@ -408,6 +411,16 @@ class DataSource(CloudInitPickleMixin, metaclass=abc.ABCMeta):
json_sensitive_file = os.path.join(
self.paths.run_dir, INSTANCE_JSON_SENSITIVE_FILE
)
+ cloud_id = instance_data["v1"].get("cloud_id", "none")
+ cloud_id_file = os.path.join(self.paths.run_dir, "cloud-id")
+ util.write_file(f"{cloud_id_file}-{cloud_id}", f"{cloud_id}\n")
+ if os.path.exists(cloud_id_file):
+ prev_cloud_id_file = os.path.realpath(cloud_id_file)
+ else:
+ prev_cloud_id_file = cloud_id_file
+ util.sym_link(f"{cloud_id_file}-{cloud_id}", cloud_id_file, force=True)
+ if prev_cloud_id_file != cloud_id_file:
+ util.del_file(prev_cloud_id_file)
write_json(json_sensitive_file, processed_data, mode=0o600)
json_file = os.path.join(self.paths.run_dir, INSTANCE_JSON_FILE)
# World readable