From 433fa811942384054b15afe6181a9d34c19308b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= Date: Fri, 9 Oct 2026 10:48:39 +0800 Subject: [PATCH] Fix MySQL diagnostic script quoting --- deploy_tool/installer.py | 40 ++++++++++++++++++++++++++-------------- tests/test_installer.py | 30 +++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/deploy_tool/installer.py b/deploy_tool/installer.py index 812b99f..b53b39d 100644 --- a/deploy_tool/installer.py +++ b/deploy_tool/installer.py @@ -558,10 +558,9 @@ def ensure_mysql_service_running( else: service_log = default_mysql_service_log_path() on_output(f"MySQL 服务启动日志: {service_log}") - exit_code = run_elevated_command_with_log( - build_mysql_service_start_command(service_name), + exit_code = run_mysql_service_start_diagnostic( + service_name, service_log, - show_window=False, ) if exit_code not in {0, 1056}: detail = _read_install_log_tail(default_mysql_service_log_path(), max_lines=24) @@ -586,9 +585,9 @@ def ensure_mysql_service_running( ) -def build_mysql_service_start_command(service_name: str = "MySQL80") -> list[str]: +def build_mysql_service_start_script(service_name: str = "MySQL80") -> str: escaped_service_name = service_name.replace("'", "''") - script = ( + return ( "$ErrorActionPreference='Continue';" f"$name='{escaped_service_name}';" "$startError='';" @@ -607,15 +606,28 @@ def build_mysql_service_start_command(service_name: str = "MySQL80") -> list[str "else{Write-Output 'No .err log found with administrator privileges.'};" "if($service.State -eq 'Running'){exit 0}else{exit 1}" ) - return [ - "powershell.exe", - "-NoProfile", - "-NonInteractive", - "-ExecutionPolicy", - "Bypass", - "-Command", - script, - ] + + +def run_mysql_service_start_diagnostic(service_name: str, log_path: Path) -> int: + with tempfile.TemporaryDirectory(prefix="autodeploy-mysql-service-") as temporary: + script_path = Path(temporary) / "start-mysql-service.ps1" + script_path.write_text( + build_mysql_service_start_script(service_name), + encoding="utf-8-sig", + ) + return run_elevated_command_with_log( + [ + "powershell.exe", + "-NoProfile", + "-NonInteractive", + "-ExecutionPolicy", + "Bypass", + "-File", + str(script_path), + ], + log_path, + show_window=False, + ) def find_mysql_error_log(mysql_executable: Path | None = None) -> Path | None: diff --git a/tests/test_installer.py b/tests/test_installer.py index 720f4fd..fabb1e1 100644 --- a/tests/test_installer.py +++ b/tests/test_installer.py @@ -22,7 +22,7 @@ from deploy_tool.installer import ( NODE_DIST_BASE, build_mysql_installer_command, build_mysql_server_install_command, - build_mysql_service_start_command, + build_mysql_service_start_script, configure_mysql_weak_password_policy, default_mysql_install_log_path, default_mysql_server_install_log_path, @@ -31,6 +31,7 @@ from deploy_tool.installer import ( find_mysql_error_log, mysql_service_failure_detail, query_mysql_service_state, + run_mysql_service_start_diagnostic, run_elevated_command_with_log, TOMCAT_VERSION, install_maven, @@ -462,10 +463,7 @@ class InstallerTests(unittest.TestCase): self.assertTrue(any("已启动" in message for message in messages)) def test_builds_privileged_mysql_service_start_diagnostic(self): - command = build_mysql_service_start_command() - - self.assertEqual(command[0], "powershell.exe") - script = command[-1] + script = build_mysql_service_start_script() self.assertIn("Start-Service -Name $name", script) self.assertIn("Get-CimInstance Win32_Service", script) self.assertIn("my.ini", script) @@ -479,6 +477,28 @@ class InstallerTests(unittest.TestCase): Path(r"C:\Users\tester\AppData\Local") / "AutoDeploy" / "logs" / "mysql-service-start.log", ) + def test_runs_mysql_service_diagnostic_from_script_file(self): + with tempfile.TemporaryDirectory() as tmp: + log_path = Path(tmp) / "mysql-service-start.log" + + def inspect_command(command, passed_log, show_window=True): + self.assertEqual(command[0], "powershell.exe") + self.assertEqual(command[-2], "-File") + script_path = Path(command[-1]) + self.assertTrue(script_path.exists()) + self.assertIn("Name='\"+$name+\"'", script_path.read_text(encoding="utf-8-sig")) + self.assertEqual(passed_log, log_path) + self.assertFalse(show_window) + return 1 + + with patch( + "deploy_tool.installer.run_elevated_command_with_log", + side_effect=inspect_command, + ): + exit_code = run_mysql_service_start_diagnostic("MySQL80", log_path) + + self.assertEqual(exit_code, 1) + def test_reports_mysql_error_log_when_service_stops_after_start(self): with tempfile.TemporaryDirectory() as tmp: program_data = Path(tmp)