From 0824825dc5392d0094138427ca61810448167ed2 Mon Sep 17 00:00:00 2001 From: axiaxixixixi <306232627@qq.com> Date: Tue, 15 Oct 2024 17:19:52 +0800 Subject: [PATCH] =?UTF-8?q?ipv6=E6=B5=81=E5=A4=B1=E5=AE=B9=E7=81=BE?= =?UTF-8?q?=E9=80=82=E9=85=8D=E5=8F=8Aping6=E5=91=BD=E4=BB=A4=E8=A1=A5?= =?UTF-8?q?=E5=85=85=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/base_utils/os/cmd_util.py | 16 ++++++++++++++-- script/base_utils/os/net_util.py | 13 +++++++++++++ script/gs_sshexkey | 6 +++--- script/gspylib/common/Common.py | 11 ++++++----- .../gspylib/component/Kernel/DN_OLAP/DN_OLAP.py | 2 +- script/gspylib/component/Kernel/Kernel.py | 5 +++-- script/impl/expansion/ExpansionImpl.py | 9 +++------ script/local/ConfigHba.py | 3 +-- 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/script/base_utils/os/cmd_util.py b/script/base_utils/os/cmd_util.py index 56cba19..ec2de18 100644 --- a/script/base_utils/os/cmd_util.py +++ b/script/base_utils/os/cmd_util.py @@ -45,7 +45,9 @@ class CmdUtil(object): ENV_SOURCE_CMD = "source /etc/profile;source ~/.bashrc;" \ "if [ $MPPDB_ENV_SEPARATE_PATH ]; " \ "then source $MPPDB_ENV_SEPARATE_PATH; fi" - + PING_IPV4_TOOL = "ping" + PING_IPV6_TOOL = "ping6" + @staticmethod def execCmd(cmd, noexcept=False): """ @@ -200,12 +202,22 @@ class CmdUtil(object): input : host, count, interval, packet_size output : str """ + ping_tool = CmdUtil.PING_IPV4_TOOL + if os.getenv("IP_TYPE") == "ipv6": + ping_tool = CmdUtil.PING_IPV6_TOOL opts = " " if int(packet_size) != int(56): opts = " -s " + str(packet_size) - return CmdUtil.findCmdInPath('ping') + BLANK_SPACE + host + " -c " + \ + return CmdUtil.findCmdInPath(ping_tool) + BLANK_SPACE + host + " -c " + \ count + " -i " + interval + opts + @staticmethod + def get_ping_tool(): + ping_tool = CmdUtil.PING_IPV4_TOOL + if os.getenv("IP_TYPE") == "ipv6": + ping_tool = CmdUtil.PING_IPV6_TOOL + return ping_tool + @staticmethod def getWcCmd(): """ diff --git a/script/base_utils/os/net_util.py b/script/base_utils/os/net_util.py index 55a26d9..609b1ce 100644 --- a/script/base_utils/os/net_util.py +++ b/script/base_utils/os/net_util.py @@ -171,6 +171,19 @@ class NetUtil(object): else: return NetUtil.IPV4_SUBMASK_LEN + @staticmethod + def get_ip_cidr_segment(self, ip_address): + # Determine the IP type based on the environment variable + if os.getenv("IP_TYPE") == NetUtil.NET_IPV4: + ip_split = '.' + cidr_segment = ".0.0/16" # IPv4 Default subnet segment + elif os.getenv("IP_TYPE") == NetUtil.NET_IPV6: + ip_split = ':' + cidr_segment = "::/64" # IPv6 Default subnet segment + else: + return "" + return ip_split.join(ip_address.split(ip_split)[:2]) + cidr_segment + @staticmethod def executePingCmd(ip_address): """ diff --git a/script/gs_sshexkey b/script/gs_sshexkey index 1d028b4..8f6b94b 100644 --- a/script/gs_sshexkey +++ b/script/gs_sshexkey @@ -849,10 +849,10 @@ General options: # add hostname when using root user for _, hostname in result.items(): try: - host = socket.gethostbyname(hostname) + addr_info = socket.getaddrinfo(hostname, None, socket.AF_UNSPEC) except Exception as e: - host = "" - if host: + addr_info = "" + if addr_info: hostnameList.append(hostname) for hostname in hostnameList: cmd = '%s;/usr/bin/ssh-keyscan -t ed25519 %s >> %s ' % (SYSTEM_SSH_ENV, hostname, self.known_hosts_fname) diff --git a/script/gspylib/common/Common.py b/script/gspylib/common/Common.py index b601314..60ec96c 100644 --- a/script/gspylib/common/Common.py +++ b/script/gspylib/common/Common.py @@ -1714,7 +1714,7 @@ class DefaultValue(): """ ping node with short timeout """ - cmd = "ping %s -c 1 -w 4" % node_ip + cmd = "%s %s -c 1 -w 4" % (CmdUtil.get_ping_tool(), node_ip) proc = FastPopen(cmd, stdout=PIPE, stderr=PIPE, preexec_fn=os.setsid, close_fds=True) proc.communicate() status = proc.returncode @@ -1726,7 +1726,8 @@ class DefaultValue(): """ Ping on remote node with -I """ - cmd = "ping %s -c 1 -w 4" % on_node + ping_tool = CmdUtil.get_ping_tool() + cmd = "%s %s -c 1 -w 4" % (ping_tool, on_node) proc = FastPopen(cmd, stdout=PIPE, stderr=PIPE, preexec_fn=os.setsid, close_fds=True) proc.communicate() @@ -1735,10 +1736,10 @@ class DefaultValue(): logger.debug("Node:%s ping failed, can not execute remote check." % on_node) return on_node, False if on_node == NetUtil.GetHostIpOrName(): - cmd_remote = "ping %s -I %s -c 1 -w 4" % (to_ip, from_ip) + cmd_remote = "%s %s -I %s -c 1 -w 4" % (ping_tool, to_ip, from_ip) else: - cmd_remote = "source %s && pssh -s -H %s 'ping %s -I %s -c 1 -w 4'" \ - % (EnvUtil.getMpprcFile(), on_node, to_ip, from_ip) + cmd_remote = "source %s && pssh -s -H %s '%s %s -I %s -c 1 -w 4'" \ + % (EnvUtil.getMpprcFile(), on_node, ping_tool, to_ip, from_ip) proc = FastPopen(cmd_remote, stdout=PIPE, stderr=PIPE, preexec_fn=os.setsid, close_fds=True) proc.communicate() diff --git a/script/gspylib/component/Kernel/DN_OLAP/DN_OLAP.py b/script/gspylib/component/Kernel/DN_OLAP/DN_OLAP.py index a720504..870ddbf 100644 --- a/script/gspylib/component/Kernel/DN_OLAP/DN_OLAP.py +++ b/script/gspylib/component/Kernel/DN_OLAP/DN_OLAP.py @@ -548,7 +548,7 @@ class DN_OLAP(Kernel): % (pg_user, dn_ip, subnet_length, METHOD_TRUST) guc_paras_str += "-h \"host all all %s/%s %s\" " \ % (dn_ip, subnet_length, METHOD_SHA) - ip_segment = '.'.join(dn_ip.split('.')[:2]) + ".0.0/16" + ip_segment = NetUtil.get_ip_cidr_segment(dn_ip) guc_paras_str += "-h \"host replication all %s sha256\" " % ip_segment if (guc_paras_str != ""): diff --git a/script/gspylib/component/Kernel/Kernel.py b/script/gspylib/component/Kernel/Kernel.py index ec24891..e54804e 100644 --- a/script/gspylib/component/Kernel/Kernel.py +++ b/script/gspylib/component/Kernel/Kernel.py @@ -148,6 +148,7 @@ class Kernel(BaseComponent): def build(self, buidMode="full", standByBuildTimeout=300): """ """ + ping_tool = CmdUtil.get_ping_tool() cmd = "%s/gs_ctl build -D %s -M standby -b %s -r %d " % ( self.binPath, self.instInfo.datadir, buidMode, standByBuildTimeout) (status, output) = subprocess.getstatusoutput(cmd) @@ -160,11 +161,11 @@ class Kernel(BaseComponent): raise Exception("cat /etc/hosts failed! cmd: %s; Error: %s " % (hostname_cmd, result)) host_list = result.splitlines() for host in host_list: - ping_cmd = f"ping {host} -c 5" + ping_cmd = f"{ping_tool} {host} -c 5" (status, result) = subprocess.getstatusoutput(ping_cmd) self.logger.debug("cmd is %s; output: %s" % (ping_cmd, result)) if status != 0: - raise Exception(f"ping {host} failed! {output}") + raise Exception(f"{ping_tool} {host} failed! {output}") raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % cmd + " Error: \n%s " % output) diff --git a/script/impl/expansion/ExpansionImpl.py b/script/impl/expansion/ExpansionImpl.py index f0df506..d4aab23 100644 --- a/script/impl/expansion/ExpansionImpl.py +++ b/script/impl/expansion/ExpansionImpl.py @@ -1462,13 +1462,10 @@ remoteservice={remoteservice}'"\ check if network delay greater than 1000ms """ backips = self.context.newHostList + ping_tool = CmdUtil.get_ping_tool() for backip in backips: - if NetUtil.get_ip_version(backip) == NetUtil.NET_IPV6: - ck_net_delay = "ping6 -s 8192 -c 5 -i 0.3 %s | " \ - "awk -F / '{print $5}'| awk '{print $1}'" % backip - else: - ck_net_delay = "ping -s 8192 -c 5 -i 0.3 %s | "\ - "awk -F / '{print $5}'| awk '{print $1}'" % backip + ck_net_delay = "%s -s 8192 -c 5 -i 0.3 %s | "\ + "awk -F / '{print $5}'| awk '{print $1}'" % (ping_tool, backip) (status, output) = subprocess.getstatusoutput(ck_net_delay) if status == 0: try: diff --git a/script/local/ConfigHba.py b/script/local/ConfigHba.py index e0db8d1..bae1932 100644 --- a/script/local/ConfigHba.py +++ b/script/local/ConfigHba.py @@ -230,8 +230,7 @@ class ConfigHba(LocalBaseOM): """ remove dn & cn pg_hba for streaming stop """ - ip_segment_list = list(set(['.'.join( - remove_ip.split('.')[:2]) + ".0.0/16" for remove_ip in self.removeIps])) + ip_segment_list = list(set([NetUtil.get_ip_cidr_segment(remove_ip) for remove_ip in self.removeIps])) for ip_segment in ip_segment_list: ip_remove_str = "-h \"host replication all %s\" " % ip_segment component.doGUCConfig("set", ip_remove_str, True)