diff --git a/apps/backend/agent/tasks.py b/apps/backend/agent/tasks.py index 8c2603434..e6121f87e 100644 --- a/apps/backend/agent/tasks.py +++ b/apps/backend/agent/tasks.py @@ -97,7 +97,7 @@ def arg_parser() -> argparse.ArgumentParser: cmd_str=cmd_str, ) params = "-l {login_ip} -p {port} -a {account} -i {identity} -d {download_url}".format( - login_ip=host.login_ip, + login_ip=host.login_ip or host.inner_ip or host.inner_ipv6, port=host.identity.port, account=host.identity.account, identity=host.identity.key if host.identity.auth_type == constants.AuthType.KEY else host.identity.password, @@ -161,7 +161,7 @@ def arg_parser() -> argparse.ArgumentParser: else: output = execute_cmd( f"type {dest_dir}nm.setup_agent.bat.{node_id}", - host.login_ip or host.inner_ip, + host.login_ip or host.inner_ip or host.inner_ipv6, host.identity.account, host.identity.password, )["data"] diff --git a/apps/backend/components/collections/agent_new/add_or_update_hosts.py b/apps/backend/components/collections/agent_new/add_or_update_hosts.py index 09097d865..e0d31c91f 100644 --- a/apps/backend/components/collections/agent_new/add_or_update_hosts.py +++ b/apps/backend/components/collections/agent_new/add_or_update_hosts.py @@ -428,13 +428,6 @@ def handle_update_db(self, sub_insts: List[models.SubscriptionInstanceRecord]): outer_ipv6: str = host_info.get("bk_host_outerip_v6") or "" login_ip: str = host_info.get("login_ip") or "" - if host_info["host_node_type"] == constants.NodeType.PROXY: - # Proxy login_ip 为空的情况取值顺序:外网 IP(v4 -> v6)-> 内网 IP(v4 -> v6) - login_ip = login_ip or outer_ip or outer_ipv6 or inner_ip or inner_ipv6 - else: - # 其他情况下:内网 IP(v4 -> v6) - login_ip = login_ip or inner_ip or inner_ipv6 - extra_data = { "peer_exchange_switch_for_agent": host_info.get("peer_exchange_switch_for_agent", 0), "bt_speed_limit": host_info.get("bt_speed_limit", 0), diff --git a/apps/backend/components/collections/agent_new/install.py b/apps/backend/components/collections/agent_new/install.py index 227f38cce..20b95d329 100644 --- a/apps/backend/components/collections/agent_new/install.py +++ b/apps/backend/components/collections/agent_new/install.py @@ -446,7 +446,7 @@ def execute_windows_commands( self, sub_inst_id: int, host: models.Host, commands: List[str], identity_data: models.IdentityData ): # windows command executing - ip = host.login_ip or host.inner_ip + ip = host.login_ip or host.inner_ip or host.inner_ipv6 if (identity_data.auth_type == constants.AuthType.PASSWORD and not identity_data.password) or ( identity_data.auth_type == constants.AuthType.KEY and not identity_data.key ): diff --git a/apps/backend/components/collections/agent_new/register_host.py b/apps/backend/components/collections/agent_new/register_host.py index 613628016..8dc6ca6f2 100644 --- a/apps/backend/components/collections/agent_new/register_host.py +++ b/apps/backend/components/collections/agent_new/register_host.py @@ -336,11 +336,6 @@ def handle_update_db( inner_ip = host_info["bk_host_innerip"] outer_ip = host_info.get("bk_host_outerip", "") login_ip = host_info.get("login_ip", "") - # 写入数据库 - if host_info["host_node_type"] == constants.NodeType.PROXY: - login_ip = login_ip or outer_ip or inner_ip - else: - login_ip = login_ip or inner_ip extra_data = { "peer_exchange_switch_for_agent": host_info.get("peer_exchange_switch_for_agent", 0), diff --git a/apps/backend/components/collections/common/remote.py b/apps/backend/components/collections/common/remote.py index beaf40207..94c5899a2 100644 --- a/apps/backend/components/collections/common/remote.py +++ b/apps/backend/components/collections/common/remote.py @@ -60,7 +60,13 @@ def conns_init_params(self) -> typing.Dict: :return: """ if self.host.node_type == constants.NodeType.PROXY: - ip = self.host.login_ip or self.host.outer_ip or self.host.outer_ipv6 + ip = ( + self.host.login_ip + or self.host.outer_ip + or self.host.outer_ipv6 + or self.host.inner_ip + or self.host.inner_ipv6 + ) else: ip = self.host.login_ip or self.host.inner_ip or self.host.inner_ipv6 diff --git a/apps/backend/tests/components/collections/agent_new/test_add_or_update_hosts.py b/apps/backend/tests/components/collections/agent_new/test_add_or_update_hosts.py index 5fb95fa69..4ef65490e 100644 --- a/apps/backend/tests/components/collections/agent_new/test_add_or_update_hosts.py +++ b/apps/backend/tests/components/collections/agent_new/test_add_or_update_hosts.py @@ -325,3 +325,26 @@ def assert_in_teardown(self): ) ) super().assert_in_teardown() + + +class UpdateOldWhenNoLoginInParamTestCase(AddOrUpdateHostsTestCase): + @classmethod + def adjust_test_data_in_db(cls): + super().adjust_test_data_in_db() + + # 假设用户不填写login_ip参数 + for sub_inst_obj in cls.obj_factory.sub_inst_record_objs: + sub_inst_obj.instance_info["host"].pop("login_ip") + models.SubscriptionInstanceRecord.objects.bulk_update( + cls.obj_factory.sub_inst_record_objs, fields=["instance_info"] + ) + + def assert_in_teardown(self): + super().assert_in_teardown() + self.assertEqual(models.Host.objects.filter(login_ip="").count(), self.obj_factory.init_host_num) + + +class UpdateOldWhenIncludeLoginInParamTestCase(AddOrUpdateHostsTestCase): + def assert_in_teardown(self): + super().assert_in_teardown() + self.assertEqual(models.Host.objects.filter(login_ip="").count(), 0) diff --git a/apps/backend/utils/ssh.py b/apps/backend/utils/ssh.py index 42129ebd6..86de053ed 100644 --- a/apps/backend/utils/ssh.py +++ b/apps/backend/utils/ssh.py @@ -406,9 +406,9 @@ def __init__(self, host: Host, log, identity_data: Optional[IdentityData] = None # 初始化ssh会话 if host.node_type == constants.NodeType.PROXY: - ip = host.login_ip or host.outer_ip + ip = host.login_ip or host.outer_ip or host.outer_ipv6 or host.inner_ip or host.inner_ipv6 else: - ip = host.login_ip or host.inner_ip + ip = host.login_ip or host.inner_ip or host.inner_ipv6 identity_data = identity_data or host.identity if (identity_data.auth_type == AuthType.PASSWORD and not identity_data.password) or ( identity_data.auth_type == AuthType.KEY and not identity_data.key