From: Yuxiang Zhu Date: Mon, 2 Aug 2021 17:03:11 +0000 (+0800) Subject: cephadm: better port stripping X-Git-Tag: v16.2.6~25^2~8 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=7e9bf74c394af799eb2ce76cd0973ee04326d3ce;p=ceph.git cephadm: better port stripping in case the specified port number contains leading zeros Signed-off-by: Yuxiang Zhu (cherry picked from commit 496a2c40c87858c188467b7dbacf5e2385ad521c) --- diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 4be920db8e0c5..3bb98dbf6c705 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -3587,7 +3587,8 @@ def prepare_mon_addresses( ctx.mon_ip = wrap_ipv6(ctx.mon_ip) hasport = r.findall(ctx.mon_ip) if hasport: - port = int(hasport[0]) + port_str = hasport[0] + port = int(port_str) if port == 6789: addr_arg = '[v1:%s]' % ctx.mon_ip elif port == 3300: @@ -3596,7 +3597,7 @@ def prepare_mon_addresses( logger.warning('Using msgr2 protocol for unrecognized port %d' % port) addr_arg = '[v2:%s]' % ctx.mon_ip - base_ip = ctx.mon_ip[0:-(len(str(port))) - 1] + base_ip = ctx.mon_ip[0:-(len(port_str)) - 1] check_ip_port(ctx, base_ip, port) else: base_ip = ctx.mon_ip @@ -3614,10 +3615,11 @@ def prepare_mon_addresses( if not hasport: raise Error('--mon-addrv value %s must include port number' % addr_arg) - port = int(hasport[0]) + port_str = hasport[0] + port = int(port_str) # strip off v1: or v2: prefix addr = re.sub(r'^v\d+:', '', addr) - base_ip = addr[0:-(len(str(port))) - 1] + base_ip = addr[0:-(len(port_str)) - 1] check_ip_port(ctx, base_ip, port) else: raise Error('must specify --mon-ip or --mon-addrv') diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index 7a91f4b4b85cf..cd5ed98933ab6 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -1280,6 +1280,11 @@ class TestBootstrap(object): {'192.168.1.0/24': {'eth0': ['192.168.1.1']}}, True, ), + ( + '192.168.1.1:0123', + {'192.168.1.0/24': {'eth0': ['192.168.1.1']}}, + True, + ), # IPv6 ( '::', @@ -1301,6 +1306,16 @@ class TestBootstrap(object): {"ffff::/64": {"eth0": ["::ffff:c0a8:101"]}}, True, ), + ( + '[::ffff:c0a8:101]:1234', + {"ffff::/64": {"eth0": ["::ffff:c0a8:101"]}}, + True, + ), + ( + '[::ffff:c0a8:101]:0123', + {"ffff::/64": {"eth0": ["::ffff:c0a8:101"]}}, + True, + ), ( '0000:0000:0000:0000:0000:FFFF:C0A8:0101', {"ffff::/64": {"eth0": ["::ffff:c0a8:101"]}}, @@ -1337,6 +1352,11 @@ class TestBootstrap(object): {'192.168.1.0/24': {'eth0': ['192.168.1.1']}}, None, ), + ( + '[192.168.1.1:0123]', + {'192.168.1.0/24': {'eth0': ['192.168.1.1']}}, + None, + ), ( '[v2:192.168.1.1:3300,v1:192.168.1.1:6789]', {'192.168.1.0/24': {'eth0': ['192.168.1.1']}}, @@ -1348,6 +1368,11 @@ class TestBootstrap(object): {'ffff::/64': {'eth0': ['::ffff:c0a8:101']}}, None, ), + ( + '[::ffff:192.168.1.1:0123]', + {'ffff::/64': {'eth0': ['::ffff:c0a8:101']}}, + None, + ), ( '[0000:0000:0000:0000:0000:FFFF:C0A8:0101:1234]', {'ffff::/64': {'eth0': ['::ffff:c0a8:101']}},