[pve-devel] [PATCH manager 03/20] ceph: refactor mon info retrieval

Fabian Grünbichler f.gruenbichler at proxmox.com
Fri Aug 25 10:48:05 CEST 2017


so it can be reused for modifying the storage definitions

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 PVE/API2/Ceph.pm | 72 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 37 insertions(+), 35 deletions(-)

diff --git a/PVE/API2/Ceph.pm b/PVE/API2/Ceph.pm
index c4d6ffcb..d6bc4c6a 100644
--- a/PVE/API2/Ceph.pm
+++ b/PVE/API2/Ceph.pm
@@ -697,6 +697,42 @@ __PACKAGE__->register_method ({
 
     }});
 
+my $get_monitor_hash = sub {
+    my $cfg = PVE::CephTools::parse_ceph_config();
+
+    my $monhash = {};
+    foreach my $section (keys %$cfg) {
+	my $d = $cfg->{$section};
+	if ($section =~ m/^mon\.(\S+)$/) {
+	    my $monid = $1;
+	    if ($d->{'mon addr'} && $d->{'host'}) {
+		$monhash->{$monid} = {
+		    addr => $d->{'mon addr'},
+		    host => $d->{'host'},
+		    name => $monid,
+		}
+	    }
+	}
+    }
+
+    eval {
+	my $rados = PVE::RADOS->new();
+	my $monstat = $rados->mon_command({ prefix => 'mon_status' });
+	my $mons = $monstat->{monmap}->{mons};
+	foreach my $d (@$mons) {
+	    next if !defined($d->{name});
+	    $monhash->{$d->{name}}->{rank} = $d->{rank};
+	    $monhash->{$d->{name}}->{addr} = $d->{addr};
+	    if (grep { $_ eq $d->{rank} } @{$monstat->{quorum}}) {
+		$monhash->{$d->{name}}->{quorum} = 1;
+	    }
+	}
+    };
+    warn $@ if $@;
+
+    return $monhash;
+};
+
 __PACKAGE__->register_method ({
     name => 'listmon',
     path => 'mon',
@@ -729,41 +765,7 @@ __PACKAGE__->register_method ({
 
 	PVE::CephTools::check_ceph_inited();
 
-	my $res = [];
-
-	my $cfg = PVE::CephTools::parse_ceph_config();
-
-	my $monhash = {};
-	foreach my $section (keys %$cfg) {
-	    my $d = $cfg->{$section};
-	    if ($section =~ m/^mon\.(\S+)$/) {
-		my $monid = $1;
-		if ($d->{'mon addr'} && $d->{'host'}) {
-		    $monhash->{$monid} = {
-			addr => $d->{'mon addr'},
-			host => $d->{'host'},
-			name => $monid,
-		    }
-		}
-	    }
-	}
-
-	eval {
-	    my $rados = PVE::RADOS->new();
-	    my $monstat = $rados->mon_command({ prefix => 'mon_status' });
-	    my $mons = $monstat->{monmap}->{mons};
-	    foreach my $d (@$mons) {
-		next if !defined($d->{name});
-		$monhash->{$d->{name}}->{rank} = $d->{rank};
-		$monhash->{$d->{name}}->{addr} = $d->{addr};
-		if (grep { $_ eq $d->{rank} } @{$monstat->{quorum}}) {
-		    $monhash->{$d->{name}}->{quorum} = 1;
-		}
-	    }
-	};
-	warn $@ if $@;
-
-	return PVE::RESTHandler::hash_to_array($monhash, 'name');
+	return PVE::RESTHandler::hash_to_array($get_monitor_hash->(), 'name');
     }});
 
 __PACKAGE__->register_method ({
-- 
2.11.0





More information about the pve-devel mailing list