[pve-devel] [PATCH v2 pve-manager 02/10] ceph: tools: parse Ceph version in separate sub and update regex

Max Carrara m.carrara at proxmox.com
Mon Jul 1 16:10:32 CEST 2024


The part of the `get_local_version` sub that's concerned with actually
parsing the Ceph version is factored into a separate sub
`parse_ceph_version`. That way the parsing logic can easily be reused.

Make the version regex more maintainable declaring it as a variable,
breaking it up and commenting it by using the x flag.

Also remove the part that parses our Debian revision (e.g. -pve1) from
the version, as we do not actually include that in our Ceph builds.

The part of the regex that parses the build commit hash is made
mandatory (remove '?' after its group).

Signed-off-by: Max Carrara <m.carrara at proxmox.com>
Tested-by: Lukas Wagner <l.wagner at proxmox.com>
Reviewed-by: Lukas Wagner <l.wagner at proxmox.com>
---
Changes v1 --> v2:
  * parse Ceph version in separate subroutine `parse_ceph_version`
  * update comments

NOTE: Not sure if we should just `return
parse_ceph_version($ceph_version)` below - the context is "passed on"
but I don't want to introduce a pattern where others have to decipher
what a subroutine returns through another subroutine's return value.
(Unless it's really obvious.)

 PVE/Ceph/Tools.pm | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/PVE/Ceph/Tools.pm b/PVE/Ceph/Tools.pm
index 087c4ef3..90313353 100644
--- a/PVE/Ceph/Tools.pm
+++ b/PVE/Ceph/Tools.pm
@@ -68,11 +68,33 @@ sub get_local_version {
 
     return undef if !defined $ceph_version;
 
-    if ($ceph_version =~ /^ceph.*\sv?(\d+(?:\.\d+)+(?:-pve\d+)?)\s+(?:\(([a-zA-Z0-9]+)\))?/) {
+    my ($version, $buildcommit, $subversions) = parse_ceph_version($ceph_version);
+
+    return undef if !defined($version);
+
+    # return (version, buildid, [major, minor, ...]) : major;
+    return wantarray ? ($version, $buildcommit, $subversions) : $subversions->[0];
+}
+
+sub parse_ceph_version : prototype($) {
+    my ($ceph_version) = @_;
+
+    my $re_ceph_version = qr/
+	# Skip ahead to the version, which may optionally start with 'v'
+	^ceph.*\sv?
+
+	# Parse the version X.Y, X.Y.Z, etc.
+	( \d+ (?:\.\d+)+ ) \s+
+
+	# Parse the git commit hash between parentheses
+	(?: \( ([a-zA-Z0-9]+) \) )
+    /x;
+
+    if ($ceph_version =~ /$re_ceph_version/) {
 	my ($version, $buildcommit) = ($1, $2);
 	my $subversions = [ split(/\.|-/, $version) ];
 
-	# return (version, buildid, major, minor, ...) : major;
+	# return (version, buildid, [major, minor, ...]) : major;
 	return wantarray
 	    ? ($version, $buildcommit, $subversions)
 	    : $subversions->[0];
-- 
2.39.2





More information about the pve-devel mailing list