Skip to content

ENT-11346: Added the 'cfbs render-input' command - #326

Merged
larsewi merged 9 commits into
cfengine:masterfrom
larsewi:cfbs-render-input
Jul 29, 2026
Merged

ENT-11346: Added the 'cfbs render-input' command#326
larsewi merged 9 commits into
cfengine:masterfrom
larsewi:cfbs-render-input

Conversation

@larsewi

@larsewi larsewi commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Converts input data for a module into an augments file (def.json). It does the same conversion as the input build step performs during cfbs build, except that the input data is read from an infile and nothing is stored in the built project.

Mission Portal needs this to render the augment for module input which is entered per host group, and thus stored in its database rather than in the project.

$ cfbs init
 ---snip---
$ cfbs get-input uninstall-packages - | jq '.[0].response += [{"name": "curl", "why": "its cool"}]' | cfbs render-input uninstall-packages - -
{
  "variables": {
    "uninstall_packages:uninstall_packages.package_names": {
      "value": [{ "name": "curl", "why": "its cool" }],
      "comment": "Added by 'cfbs input'"
    }
  }
}

The first 3 commits are preparatory refactoring, with no functional change. The next 3 fix pre-existing crashes found while implementing this, each with a shell test which fails without its fix.

Ticket: ENT-11346

🤖 Generated with Claude Code

@larsewi larsewi changed the title Added the 'cfbs render-input' command ENT-11346: Added the 'cfbs render-input' command Jul 28, 2026
@larsewi
larsewi force-pushed the cfbs-render-input branch from 3f1169f to 9cae354 Compare July 28, 2026 15:03
larsewi added 3 commits July 28, 2026 17:09
The _compare_dict() / _compare_list() helpers in set_input_command() are
moved to validate.py as input_data_matches_spec(), so the same check can
be reused by the render-input command which will later be implemented in
ticket ENT-11346.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Moved _generate_augment() to cfbs/augments.py so that it can be reused
by the render-input command which will later be implemented in ticket
ENT-11346.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Generalized the logic in 'get-input' and 'set-input' for opening a file
and conditionally returning the stdin/stdout file descriptors. This
function will be reused by the render-input command implement in ticket
ENT-11346.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
@larsewi
larsewi force-pushed the cfbs-render-input branch from 9cae354 to e9362b4 Compare July 28, 2026 15:25
@larsewi
larsewi marked this pull request as ready for review July 28, 2026 15:48
Comment thread tests/shell/051_get_input_no_build_list.sh Outdated
larsewi added 5 commits July 29, 2026 14:26
get_module_from_build() looked up the "build" key unconditionally,
raising an exception for a project that uses "provides" instead.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
The get_module_object() function now takes a "default" argument,
returned when the module or the requested version is not in the index,
defaulting to None.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
input_data_matches_spec() zipped the input definition and the input data
together without checking that they are lists.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Converts input data for a module into an augments file (def.json). This
is the same conversion the 'input' build step performs during 'cfbs
build', except that the input data is read from an infile instead of
<module-name>/input.json, and nothing is stored in the project.

Mission Portal needs this to render the augment for module input which
is entered per host group, and thus stored in its database rather than
in the project.

Ticket: ENT-11346
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
@larsewi
larsewi force-pushed the cfbs-render-input branch from e9362b4 to e28ead6 Compare July 29, 2026 12:26
@larsewi
larsewi requested a review from olehermanse July 29, 2026 12:27
Comment thread cfbs/main.py Outdated
@larsewi
larsewi requested a review from olehermanse July 29, 2026 12:56
Comment thread cfbs/main.py Outdated
Comment thread cfbs/main.py
Comment on lines +258 to +273
# ExitStack rather than a plain with statement, so that the OSError
# handling only covers opening the file, not running the command:
with contextlib.ExitStack() as stack:
# Open the infile first, so that a mistyped infile doesn't truncate
# the outfile:
try:
infile = stack.enter_context(open_file_arg(infilename, "r"))
except OSError as e:
log.error("Can't open '%s': %s" % (infilename, e))
return 1
try:
outfile = stack.enter_context(open_file_arg(outfilename, "w"))
except OSError as e:
log.error("Can't open '%s': %s" % (outfilename, e))
return 1
return commands.render_input_command(module, infile, outfile)

@olehermanse olehermanse Jul 29, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a bit unnecessarily complicated IMO, don't know exactly what's the best way.

Something like this feels more natural for me;

Suggested change
# ExitStack rather than a plain with statement, so that the OSError
# handling only covers opening the file, not running the command:
with contextlib.ExitStack() as stack:
# Open the infile first, so that a mistyped infile doesn't truncate
# the outfile:
try:
infile = stack.enter_context(open_file_arg(infilename, "r"))
except OSError as e:
log.error("Can't open '%s': %s" % (infilename, e))
return 1
try:
outfile = stack.enter_context(open_file_arg(outfilename, "w"))
except OSError as e:
log.error("Can't open '%s': %s" % (outfilename, e))
return 1
return commands.render_input_command(module, infile, outfile)
unexpected = None
try:
with open(inp, "r") as infile:
with open(out, "w") as outfile:
try:
commands.render_input_command(module, infile, outfile)
except Exception as e:
unexpected = e
except OSError as e:
log.error("Can't open '%s': %s" % (outfilename, e))
return 1
if unexpected:
raise unexpected

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think both versions looks complicated

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Co-authored-by: Ole Herman Schumacher Elgesem <4048546+olehermanse@users.noreply.github.com>
@larsewi
larsewi force-pushed the cfbs-render-input branch from 2ac21f6 to c588d1f Compare July 29, 2026 14:19
@larsewi
larsewi merged commit e77ee37 into cfengine:master Jul 29, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants