From c6edc710f2230babef48c5375021c1b9c2cffba9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Hozza?= Date: Fri, 26 Jan 2024 17:36:15 +0100 Subject: [PATCH] stages/cloud-init: fix dumping of datasource_list key MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The stage dumps invalid cloud-init configuration, in case the `datasource_list` key has a value assigned. The value is supposed to be a list, but cloud-init documentation mandates that the value is always a single line, with no newlines. This was not true in the past. Fix #1554 Signed-off-by: Tomáš Hozza --- stages/org.osbuild.cloud-init | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/stages/org.osbuild.cloud-init b/stages/org.osbuild.cloud-init index 6361691c..aa986948 100755 --- a/stages/org.osbuild.cloud-init +++ b/stages/org.osbuild.cloud-init @@ -142,12 +142,34 @@ SCHEMA = r""" """ +# Class representing the 'datasource_list' configuration option, +# allowing to define a custom YAML dumper for it. +class DatasourceList(list): + pass + + +# Dedicated YAML dumper for the `DatasourceList` class. +# Make sure that the `DatasourceList` always uses flow style. +# https://cloudinit.readthedocs.io/en/latest/reference/base_config_reference.html#datasource-list +# Specifically, it says for the `datasource_list` key: +# "This key is unique in that it uses a subset of YAML syntax. +# It requires that the key and its contents, a list, +# must share a single line - no newlines." +def datasourcelist_presenter(dumper, data): + return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True) + + # Writes the passed `config` object as is into the configuration file in YAML format. # The validity of the `config` content is assured by the SCHEMA. def main(tree, options): filename = options.get("filename") config = options.get("config", {}) + datasource_list = config.get("datasource_list") + if datasource_list: + config["datasource_list"] = DatasourceList(datasource_list) + yaml.add_representer(DatasourceList, datasourcelist_presenter) + config_files_dir = f"{tree}/etc/cloud/cloud.cfg.d" with open(f"{config_files_dir}/{filename}", "w", encoding="utf8") as f: