Skip to content

Commit

Permalink
style: 安装/重装如果未填写登录IP不记录登录IP字段 (closed TencentBlueKing#2053)
Browse files Browse the repository at this point in the history
  • Loading branch information
ping15 authored and ZhuoZhuoCrayon committed May 29, 2024
1 parent 24414b1 commit 1f52ad0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions apps/backend/agent/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/components/collections/agent_new/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 7 additions & 1 deletion apps/backend/components/collections/common/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions apps/backend/utils/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f52ad0

Please sign in to comment.