Fix MySQL diagnostic script quoting

This commit is contained in:
王鹏
2026-10-09 10:48:39 +08:00
parent d1321d8059
commit 433fa81194
2 changed files with 51 additions and 19 deletions

View File

@@ -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 [
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",
"-Command",
script,
]
"-File",
str(script_path),
],
log_path,
show_window=False,
)
def find_mysql_error_log(mysql_executable: Path | None = None) -> Path | None:

View File

@@ -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)