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
The 'config' option allows to create cloud-init `.cfg` configuration
files under `/etc/cloud/cloud.cfg.d`. Its value is a dictionary which keys
represent filenames of `.cfg` configuration files, which will be created.
Value of each configuration file key is a dictionary representing the
cloud-init configuration.
The 'config' option allows to configure cloud-init by creating a
configuration file under `/etc/cloud/cloud.cfg.d` with the name
specified by `filename`.
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
section definition, which is not empty (must be setting a configuration
option).
Currently supported subset of cloud-init configuration:
- 'system_info' section
@ -31,36 +25,33 @@ import osbuild.api
SCHEMA = r"""
"additionalProperties": false,
"required": ["config", "filename"],
"properties": {
"filename": {
"type": "string",
"description": "Name of the cloud-init configuration file.",
"pattern": "^[\\w.-]{1,251}\\.cfg$"
},
"config": {
"additionalProperties": false,
"type": "object",
"description": "cloud-init configuration file.",
"minProperties": 1,
"patternProperties": {
"^[\\w.-]{1,251}\\.cfg$": {
"description": "cloud-init configuration",
"properties": {
"system_info": {
"additionalProperties": false,
"type": "object",
"description": "cloud-init configuration file.",
"description": "'system_info' configuration section.",
"minProperties": 1,
"properties": {
"system_info": {
"default_user": {
"additionalProperties": false,
"type": "object",
"description": "'system_info' configuration section.",
"description": "Configuration of the 'default' user created by cloud-init.",
"minProperties": 1,
"properties": {
"default_user": {
"additionalProperties": false,
"type": "object",
"description": "Configuration of the 'default' user created by cloud-init.",
"minProperties": 1,
"properties": {
"name": {
"type": "string",
"description": "username of the 'default' user."
}
}
"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.
# The validity of the `options` content is assured by the SCHEMA.
def create_configuration_file(tree, filename, options):
# 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", {})
config_files_dir = f"{tree}/etc/cloud/cloud.cfg.d"
with open(f"{config_files_dir}/{filename}", "w") as f:
yaml.dump(options, 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)
yaml.dump(config, f)
return 0

View file

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

View file

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