]> xenbits.xensource.com Git - libvirt.git/commitdiff
systemd: Support merging multiple units
authorAndrea Bolognani <abologna@redhat.com>
Thu, 28 Sep 2023 09:39:23 +0000 (11:39 +0200)
committerAndrea Bolognani <abologna@redhat.com>
Wed, 25 Oct 2023 09:42:19 +0000 (11:42 +0200)
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>
scripts/merge-systemd-units.py

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