Skip to content

Commit

Permalink
fix: windows ieod脚本从拉取变更为执行 (closed #2368)
Browse files Browse the repository at this point in the history
# Reviewed, transaction id: 14643
  • Loading branch information
ping15 authored and wyyalt committed Oct 22, 2024
1 parent adc0e88 commit 8f754d4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
11 changes: 11 additions & 0 deletions apps/backend/agent/solution_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from apps.core.script_manage.base import ScriptHook
from apps.core.script_manage.data import JUMP_SERVER_POLICY_SCRIPT_INFO
from apps.node_man import constants, models
from apps.node_man.models import GlobalSettings
from apps.utils import basic
from apps.utils.files import PathHandler

Expand Down Expand Up @@ -477,7 +478,17 @@ def build_oneline_script_hook_steps(self) -> typing.List[ExecutionSolutionStep]:
构造可直接执行的脚本钩子步骤
:return:
"""
script_hook_obj_name__script_content_map: typing.Dict[str, str] = GlobalSettings.get_config(
models.GlobalSettings.KeyEnum.AGENT_INSTALL_SCRIPT_HOOK_CONTENT.value, {}
)
script_hook_steps: typing.List[ExecutionSolutionStep] = []

# 如果全局配置中配置了钩子脚本内容,优先使用
for script_hook_obj in self.script_hook_objs:
script_hook_obj.script_info_obj.oneline = script_hook_obj_name__script_content_map.get(
script_hook_obj.script_info_obj.name, script_hook_obj.script_info_obj.oneline
)

oneline_script_hook_objs: typing.List[ScriptHook] = [
script_hook_obj for script_hook_obj in self.script_hook_objs if script_hook_obj.script_info_obj.oneline
]
Expand Down
2 changes: 2 additions & 0 deletions apps/backend/components/collections/agent_new/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,8 @@ def convert_shell_to_powershell(cls, shell_cmd):
# Convert \\ to \
shell_cmd = shell_cmd.replace("\\\\", "\\")

shell_cmd = shell_cmd.replace(" & ", " ; ")

return shell_cmd.strip()

def build_shell_to_batch_command_converter(self, steps, command_converter):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
from apps.core.remote import exceptions
from apps.core.remote.tests import base
from apps.core.remote.tests.base import AsyncMockConn
from apps.core.script_manage.data import JUMP_SERVER_POLICY_SCRIPT_INFO
from apps.core.script_manage.data import (
IEOD_ACTIVE_FIREWALL_POLICY_SCRIPT_INFO,
JUMP_SERVER_POLICY_SCRIPT_INFO,
)
from apps.core.script_manage.handlers import ScriptManageHandler
from apps.exceptions import ApiResultError
from apps.mock_data import api_mkd, common_unit
from apps.mock_data import utils as mock_data_utils
from apps.node_man import constants, models
from apps.node_man.models import GlobalSettings
from env.constants import GseVersion
from pipeline.component_framework.test import (
ComponentTestCase,
Expand Down Expand Up @@ -181,7 +185,7 @@ def start_patch(self):
with open(
os.path.join(self.DOWNLOAD_PATH, constants.SetupScriptFileName.SETUP_PAGENT_PY.value), mode="w+"
) as fs:
fs.write("哈哈哈113343ddfd🐒")
fs.write("哈哈哈113343ddfd")

def setUp(self) -> None:
self.update_callback_url()
Expand Down Expand Up @@ -1179,6 +1183,7 @@ def init_mock_clients(self):

class WindowsActiveFirewallPolicyInDirectAreaTestCase(InstallWindowsWithScriptHooksTestCase):
def adjust_db(self):
GlobalSettings.objects.filter(key=GlobalSettings.KeyEnum.AGENT_INSTALL_SCRIPT_HOOK_CONTENT.value).delete()
sub_step_obj: models.SubscriptionStep = self.obj_factory.sub_step_objs[0]
sub_step_obj.params.update(
{
Expand Down Expand Up @@ -1221,6 +1226,41 @@ def test_batch_solution(self):
)


class ActiveFirewallAddConfigTestCase(WindowsActiveFirewallPolicyInDirectAreaTestCase):
def adjust_db(self):
super().adjust_db()
GlobalSettings.set_config(
GlobalSettings.KeyEnum.AGENT_INSTALL_SCRIPT_HOOK_CONTENT.value,
{IEOD_ACTIVE_FIREWALL_POLICY_SCRIPT_INFO.name: "test"},
)

def test_batch_solution(self):
host, script_hook_objs, installation_tool, solution_parse_result = self.build_solution_result(
script_hooks=[{"name": "ieod_active_firewall_policy"}]
)

self.assertEqual(
constants.AgentWindowsDependencies.list_member_values() + ["setup_agent.bat"],
solution_parse_result["dependencies"],
)
self.assertEqual(
solution_parse_result["cmds"],
[
# 一:创建临时目录
f"mkdir {installation_tool.dest_dir}",
# 二:执行ieod_active_firewall_policy
"test",
# 三:执行安装脚本
f"{installation_tool.dest_dir}setup_agent.bat"
f" -O 48668 -E 58925 -A 58625 -V 58930 -B 10020 -S 60020 -Z 60030 -K 10030"
f' -e "127.0.0.1" -a "127.0.0.1" -k "127.0.0.1"'
f" -l http://127.0.0.1/download -r http://127.0.0.1/backend"
f" -i 0 -I {host.inner_ip} -T C:\\tmp\\ -p c:\\gse"
f" -c {solution_parse_result['params']['token']} -s {mock_data_utils.JOB_TASK_PIPELINE_ID} -N SERVER",
],
)


class WindowsActiveFirewallPolicyNotInDirectAreaTestCase(WindowsActiveFirewallPolicyInDirectAreaTestCase):
def adjust_db(self):
super().adjust_db()
Expand Down
2 changes: 2 additions & 0 deletions apps/node_man/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class KeyEnum(Enum):
IP_CHOOSER_ENABLE_SHOW_REALTIME_AGENT_STATE = "IP_CHOOSER_ENABLE_SHOW_REALTIME_AGENT_STATE"
# IP选择器详情接口实时展示agent状态业务白名单
IP_CHOOSER_BIZ_WHITELIST = "IP_CHOOSER_BIZ_WHITELIST"
# 安装agent脚本钩子执行内容
AGENT_INSTALL_SCRIPT_HOOK_CONTENT = "AGENT_INSTALL_SCRIPT_HOOK_CONTENT"

key = models.CharField(_("键"), max_length=255, db_index=True, primary_key=True)
v_json = JSONField(_("值"))
Expand Down
2 changes: 2 additions & 0 deletions script_tools/setup_pagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ def convert_shell_to_powershell(shell_cmd):
# Convert \\ to \
shell_cmd = shell_cmd.replace("\\\\", "\\")

shell_cmd = shell_cmd.replace(" & ", " ; ")

return shell_cmd.strip()


Expand Down

0 comments on commit 8f754d4

Please sign in to comment.