we need to remove an ip, ip:port or a ipvector from monhost
so use multiple regex search and replaces for this

this looks not really nice, but due to the strange format
of the line (e.g. ',' is a seperator inside and outside of a vector,
also ipv6 adresses may be surrounded with [] but so are vectors),
i found no better way

Signed-off-by: Dominik Csapak <d.csa...@proxmox.com>
---
changes from v1:
* combined ip and ip:port regexes
 PVE/API2/Ceph/MON.pm | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/PVE/API2/Ceph/MON.pm b/PVE/API2/Ceph/MON.pm
index 37762f86..036c6374 100644
--- a/PVE/API2/Ceph/MON.pm
+++ b/PVE/API2/Ceph/MON.pm
@@ -338,6 +338,15 @@ __PACKAGE__->register_method ({
                $monstat = $rados->mon_command({ prefix => 'mon_status' });
                $monlist = $monstat->{monmap}->{mons};
 
+               my $addr;
+               for my $mon (@$monlist) {
+                   if ($mon->{name} eq $monid) {
+                       $addr = $mon->{public_addr} // $mon->{addr};
+                       ($addr) = $addr =~ m|^(.*):\d+/\d+$|; # extract the ip 
without port/nonce
+                       last;
+                   }
+               }
+
                $assert_mon_can_remove->($monhash, $monlist, $monid, $mondir);
 
                $rados->mon_command({ prefix => "mon remove", name => $monid, 
format => 'plain' });
@@ -348,6 +357,33 @@ __PACKAGE__->register_method ({
                # delete section
                delete $cfg->{$monsection};
 
+               # delete from mon_host
+               if (my $monhost = $cfg->{global}->{mon_host}) {
+                   # various replaces to remove the ip
+                   # we always match the beginning or a seperator (also at the 
end)
+                   # so we do not accidentally remove a wrong ip
+                   # e.g. removing 10.0.0.1 should not remove 10.0.0.101 or 
110.0.0.1
+
+                   # remove vector containing this ip
+                   # format is [vX:ip:port/nonce,vY:ip:port/nonce]
+                   my $vectorpart_re = "v\\d+:\Q$addr\E:\\d+\\/\\d+";
+                   $monhost =~ s/(^|[ 
,;]*)\[$vectorpart_re(?:,$vectorpart_re)*\](?:[ ,;]+|$)/$1/;
+
+                   # ip (+ port)
+                   $monhost =~ s/(^|[ ,;]+)\Q$addr\E(?::\d+)?(?:[ ,;]+|$)/$1/;
+
+                   # ipv6 only without brackets
+                   if ($addr =~ m/^\[?(.*?:.*?)\]?$/) {
+                       $addr = $1;
+                       $monhost =~ s/(^|[ ,;]+)\Q$addr\E(?:[ ,;]+|$)/$1/;
+                   }
+
+                   # remove trailing seperators
+                   $monhost =~ s/[ ,;]+$//;
+
+                   $cfg->{global}->{mon_host} = $monhost;
+               }
+
                cfs_write_file('ceph.conf', $cfg);
                File::Path::remove_tree($mondir);
                eval { PVE::Ceph::Services::ceph_service_cmd('disable', 
$monsection) };
-- 
2.11.0


_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to