systemd: Support merging multiple units

In order to further deduplicate the contents of the various unit
files, we need to be able to merge multiple additional units
into the initial one.

Luckily the merge logic is in no way constrained to working with
just two units, so achieving this is pretty much just a matter
of lifting the existing limitation on the number of arguments
that the script accepts.

As a special case, it's now also possible to call the script
with just the base unit as argument. No merging will be performed
in that case, obviously, but we'll still go through the basic
validation and cleanup steps.

This also fixes a bug in the check for the number of arguments:
sys.argv also contains the name of the script, so we should have
checked that its size was at least 3. The check is now written in
a way that's less prone to misunderstandings.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Andrea Bolognani 2023-09-28 11:39:23 +02:00
parent 9b26834305
commit b1aeca5db0

View File

@ -82,13 +82,18 @@ def merge_units(base, extra):
return merged return merged
if len(sys.argv) < 2: prog = sys.argv[0]
print("usage: {} BASE EXTRA".format(sys.argv[0])) args = sys.argv[1:]
if len(args) < 1:
print("usage: {} BASE [EXTRA]...".format(prog))
sys.exit(1) sys.exit(1)
base = parse_unit(sys.argv[1]) merged = parse_unit(args[0])
extra = parse_unit(sys.argv[2])
merged = merge_units(base, extra) for arg in args[1:]:
extra = parse_unit(arg)
merged = merge_units(merged, extra)
sys.stdout.write(format_unit(merged)) sys.stdout.write(format_unit(merged))