]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/CephConfig.pm
CephConfig: refactor host port parsing
[pve-storage.git] / PVE / CephConfig.pm
index aad2701be04aaf9b9f8b8d9c01a7f7769ed35e40..31bb887d56386b8a8bc761009044d61941664966 100644 (file)
@@ -21,8 +21,9 @@ sub parse_ceph_config {
     my $section;
 
     foreach my $line (@lines) {
-       $line =~ s/[;#].*$//;
+       $line =~ s/#.*$//;
        $line =~ s/^\s+//;
+       $line =~ s/^;.*$//;
        $line =~ s/\s+$//;
        next if !$line;
 
@@ -33,7 +34,10 @@ sub parse_ceph_config {
        }
 
        if ($line =~ m/^(.*?\S)\s*=\s*(\S.*)$/) {
-           $cfg->{$section}->{$1} = $2;
+           my ($key, $val) = ($1, $2);
+           # ceph treats ' ', '_' and '-' in keys the same, so lets do too
+           $key =~ s/[-\ ]/_/g;
+           $cfg->{$section}->{$key} = $val;
        }
 
     }
@@ -96,32 +100,37 @@ my $ceph_get_key = sub {
     return $secret;
 };
 
+my $get_host = sub {
+    my ($hostport) = @_;
+    my ($host, $port) = PVE::Tools::parse_host_and_port($hostport);
+    if (!defined($host)) {
+       return "";
+    }
+    $port = defined($port) ? ":$port" : '';
+    $host = "[$host]" if Net::IP::ip_is_ipv6($host);
+    return "${host}${port}";
+};
+
 sub get_monaddr_list {
     my ($configfile) = shift;
 
-    my $server;
-
     if (!defined($configfile)) {
        warn "No ceph config specified\n";
        return;
     }
 
     my $config = $parse_ceph_file->($configfile);
-    @$server = sort map { $config->{$_}->{'mon addr'} } grep {/mon\./} %{$config};
 
-    return join(',', @$server);
+    my @monids = grep { /mon\./ && defined($config->{$_}->{'mon addr'}) } %{$config};
+
+    return join(',', sort map { $config->{$_}->{'mon addr'} } @monids);
 };
 
 sub hostlist {
     my ($list_text, $separator) = @_;
 
     my @monhostlist = PVE::Tools::split_list($list_text);
-    return join($separator, map {
-       my ($host, $port) = PVE::Tools::parse_host_and_port($_);
-       $port = defined($port) ? ":$port" : '';
-       $host = "[$host]" if Net::IP::ip_is_ipv6($host);
-       "${host}${port}"
-    } @monhostlist);
+    return join($separator, map { $get_host->($_) } @monhostlist);
 }
 
 my $ceph_check_keyfile = sub {