stages/cloud-init: write only one config file

Instead of supporting writing an arbitrary number of configuration
files, just write one. This makes the stage and its schema clearer
and simpler. If more than one config file is needed, the stage can
be repeated multiple times. It is also more flexible since we can
in the future specify the directory, `/etc` vs `/usr` via a new
top level `directory` key.
This commit is contained in:
Christian Kellner 2021-07-21 11:16:42 +02:00
parent 0becf66454
commit a1703dc298
3 changed files with 33 additions and 48 deletions

View file

@ -2,18 +2,12 @@
""" """
Configure cloud-init Configure cloud-init
The 'config' option allows to create cloud-init `.cfg` configuration The 'config' option allows to configure cloud-init by creating a
files under `/etc/cloud/cloud.cfg.d`. Its value is a dictionary which keys configuration file under `/etc/cloud/cloud.cfg.d` with the name
represent filenames of `.cfg` configuration files, which will be created. specified by `filename`.
Value of each configuration file key is a dictionary representing the
cloud-init configuration.
Constrains: Constrains:
- If 'config' option is specified, it must contain at least one definition
of a configuration file.
- Each configuration file definition must contain at least one configuration - Each configuration file definition must contain at least one configuration
section definition, which is not empty (must be setting a configuration
option).
Currently supported subset of cloud-init configuration: Currently supported subset of cloud-init configuration:
- 'system_info' section - 'system_info' section
@ -31,36 +25,33 @@ import osbuild.api
SCHEMA = r""" SCHEMA = r"""
"additionalProperties": false, "additionalProperties": false,
"required": ["config", "filename"],
"properties": { "properties": {
"filename": {
"type": "string",
"description": "Name of the cloud-init configuration file.",
"pattern": "^[\\w.-]{1,251}\\.cfg$"
},
"config": { "config": {
"additionalProperties": false, "additionalProperties": false,
"type": "object", "type": "object",
"description": "cloud-init configuration file.", "description": "cloud-init configuration",
"minProperties": 1, "properties": {
"patternProperties": { "system_info": {
"^[\\w.-]{1,251}\\.cfg$": {
"additionalProperties": false, "additionalProperties": false,
"type": "object", "type": "object",
"description": "cloud-init configuration file.", "description": "'system_info' configuration section.",
"minProperties": 1, "minProperties": 1,
"properties": { "properties": {
"system_info": { "default_user": {
"additionalProperties": false, "additionalProperties": false,
"type": "object", "type": "object",
"description": "'system_info' configuration section.", "description": "Configuration of the 'default' user created by cloud-init.",
"minProperties": 1, "minProperties": 1,
"properties": { "properties": {
"default_user": { "name": {
"additionalProperties": false, "type": "string",
"type": "object", "description": "username of the 'default' user."
"description": "Configuration of the 'default' user created by cloud-init.",
"minProperties": 1,
"properties": {
"name": {
"type": "string",
"description": "username of the 'default' user."
}
}
} }
} }
} }
@ -72,20 +63,16 @@ SCHEMA = r"""
""" """
# Writes the passed `options` object as is into the configuration file in YAML format. # Writes the passed `config` object as is into the configuration file in YAML format.
# The validity of the `options` content is assured by the SCHEMA. # The validity of the `config` content is assured by the SCHEMA.
def create_configuration_file(tree, filename, options): def main(tree, options):
filename = options.get("filename")
config = options.get("config", {})
config_files_dir = f"{tree}/etc/cloud/cloud.cfg.d" config_files_dir = f"{tree}/etc/cloud/cloud.cfg.d"
with open(f"{config_files_dir}/{filename}", "w") as f: with open(f"{config_files_dir}/{filename}", "w") as f:
yaml.dump(options, f) yaml.dump(config, f)
def main(tree, options):
configuration_files_options = options.get("config", {})
for configuration_file, configuration_options in configuration_files_options.items():
create_configuration_file(tree, configuration_file, configuration_options)
return 0 return 0

View file

@ -506,12 +506,11 @@
{ {
"name": "org.osbuild.cloud-init", "name": "org.osbuild.cloud-init",
"options": { "options": {
"filename": "00-default_user.cfg",
"config": { "config": {
"00-default_user.cfg": { "system_info": {
"system_info": { "default_user": {
"default_user": { "name": "ec2-user"
"name": "ec2-user"
}
} }
} }
} }

View file

@ -32,12 +32,11 @@
{ {
"name": "org.osbuild.cloud-init", "name": "org.osbuild.cloud-init",
"options": { "options": {
"filename": "00-default_user.cfg",
"config": { "config": {
"00-default_user.cfg": { "system_info": {
"system_info": { "default_user": {
"default_user": { "name": "ec2-user"
"name": "ec2-user"
}
} }
} }
} }