From 822c51853412c70e940febdaa05b4421f308a3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= Date: Fri, 9 Oct 2026 11:16:54 +0800 Subject: [PATCH] Recover empty MySQL data directories with stable log names --- README.md | 2 + deploy_tool/installer.py | 145 +++++++++++------- deploy_tool/mysql_recovery.py | 138 +++++++++++++++++ tests/test_installer.py | 31 +++- tests/test_mysql_recovery.py | 272 ++++++++++++++++++++++++++++++++++ 5 files changed, 530 insertions(+), 58 deletions(-) create mode 100644 deploy_tool/mysql_recovery.py create mode 100644 tests/test_mysql_recovery.py diff --git a/README.md b/README.md index ded493d..076feb5 100644 --- a/README.md +++ b/README.md @@ -109,3 +109,5 @@ python -m unittest discover -s tests -p "test_*.py" ## 当前范围 当前版本聚焦 Windows 本机的开发/演示环境部署。远程服务器上传、Nginx 配置、Docker 部署、生产环境进程守护和凭据托管暂不在本版本范围内。 + +MySQL 服务启动失败时,工具读取该 Windows 服务实际使用的 `my.ini` 和 `datadir`。仅当服务完全停止且数据目录为空时,自动备份配置、使用英文日志文件名并初始化,避免中文主机名生成的 binlog 索引路径导致安装失败。初始化成功后启动服务并设置默认密码;目录内已有数据库或其他文件时不会重新初始化或改名。原始配置备份在 `my.ini.autodeploy-*.bak`,诊断日志在 `%LOCALAPPDATA%\AutoDeploy\logs\mysql-service-start.log`。 diff --git a/deploy_tool/installer.py b/deploy_tool/installer.py index 0e93dd2..4225b6b 100644 --- a/deploy_tool/installer.py +++ b/deploy_tool/installer.py @@ -16,6 +16,7 @@ from pathlib import Path from typing import BinaryIO, Callable from .scanner import ScanResult +from .mysql_recovery import MYSQL_RECOVERY_HELPERS JDK_PACKAGES = { @@ -525,6 +526,15 @@ def _read_install_log_tail(log_path: Path, max_lines: int = 20) -> str: return "\n".join(lines[-max_lines:]) +def _read_mysql_recovery_summary(log_path: Path) -> str: + lines = _read_install_log_tail(log_path, max_lines=300).splitlines() + causes = [line for line in lines if any(marker in line.lower() for marker in ( + "[error]", "not found", "recovery failed:", "not empty", "existing entries:", + "configuration backup:", "data directory:", "my.ini:", + ))] + return "\n".join(causes[:14] or lines[-20:]) + + def query_mysql_service_state(service_name: str = "MySQL80") -> int | None: if os.name != "nt": return 4 @@ -573,6 +583,11 @@ def ensure_mysql_service_running( service_log, mysql_server_executable=server_executable, ) + if exit_code == 2: + raise RuntimeError( + f"MySQL 初始化或配置失败。详细日志: {service_log}\n" + f"{_read_mysql_recovery_summary(service_log)}" + ) if exit_code not in {0, 1056}: on_output( f"Windows 服务启动命令返回 {exit_code},服务可能仍在后台恢复,继续等待运行状态..." @@ -589,7 +604,7 @@ def ensure_mysql_service_running( ) time.sleep(retry_delay) detail = mysql_service_failure_detail(mysql_executable, state) - diagnostic = _read_install_log_tail(default_mysql_service_log_path(), max_lines=24) + diagnostic = _read_mysql_recovery_summary(default_mysql_service_log_path()) diagnostic_text = f"\n启动诊断:\n{diagnostic}" if diagnostic else "" raise RuntimeError( f"Windows 服务 {service_name} 启动超时,启动命令退出码: {exit_code}。" @@ -604,62 +619,78 @@ def build_mysql_service_start_script( escaped_service_name = service_name.replace("'", "''") escaped_mysql_executable = str(mysql_server_executable or "").replace("'", "''") return ( - "$ErrorActionPreference='Continue';" - f"$name='{escaped_service_name}';" - f"$mysqlExe='{escaped_mysql_executable}';" - "$startError='';" - "try { Start-Service -Name $name -ErrorAction Stop } " - "catch { $startError=($_ | Out-String); Write-Output $startError };" - "Start-Sleep -Seconds 3;" - "$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");" - "$service | Select-Object Name,State,StartName,PathName,ExitCode,ProcessId | " - "Format-List | Out-String | Write-Output;" - "$ini=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\my.ini';" - "Write-Output ('my.ini: '+$ini+' exists='+[bool](Test-Path -LiteralPath $ini));" - "$data=Join-Path $env:ProgramData 'MySQL\\MySQL Server 8.0\\Data';" - "Write-Output ('Data directory: '+$data+' exists='+[bool](Test-Path -LiteralPath $data));" - "if(Test-Path -LiteralPath $data){" - "Write-Output 'Granting MySQL service account access to the data directory...';" - "& icacls.exe $data /grant '*S-1-5-20:(OI)(CI)M' /T /C 2>&1 | Out-String | Write-Output;" - "try { Start-Service -Name $name -ErrorAction Stop } catch { Write-Output ($_ | Out-String) };" - "Start-Sleep -Seconds 8;" - "$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");" - "Write-Output ('Service state after permission repair: '+$service.State)" - "};" - "$err=Get-ChildItem -LiteralPath $data -Filter '*.err' -File -ErrorAction SilentlyContinue | " - "Sort-Object LastWriteTime -Descending | Select-Object -First 1;" - "if($err){Write-Output ('Error log: '+$err.FullName); Get-Content -LiteralPath $err.FullName -Tail 30}" - "else{Write-Output 'No .err log found with administrator privileges.'};" - "if($mysqlExe -and (Test-Path -LiteralPath $mysqlExe)){" - "Write-Output ('mysqld.exe: '+$mysqlExe);" - "Write-Output 'mysqld version:'; & $mysqlExe --version 2>&1 | Out-String | Write-Output;" - "Write-Output 'mysqld configuration validation:';" - "& $mysqlExe (\"--defaults-file=\"+$ini) --validate-config --console 2>&1 | Out-String | Write-Output;" - "Write-Output ('validate-config exit code: '+$LASTEXITCODE)" - "};" - "$systemDatabase=Join-Path $data 'mysql';" - "if($service.State -ne 'Running' -and $mysqlExe -and " - "(Test-Path -LiteralPath $mysqlExe) -and -not (Test-Path -LiteralPath $systemDatabase)){" - "Write-Output 'MySQL system database is missing; attempting safe initialization without deleting data...';" - "& $mysqlExe (\"--defaults-file=\"+$ini) --initialize-insecure --console 2>&1 | Out-String | Write-Output;" - "Write-Output ('initialize-insecure exit code: '+$LASTEXITCODE);" - "& icacls.exe $data /grant '*S-1-5-20:(OI)(CI)M' /T /C 2>&1 | Out-String | Write-Output;" - "try { Start-Service -Name $name -ErrorAction Stop } catch { Write-Output ($_ | Out-String) };" - "Start-Sleep -Seconds 12;" - "$service=Get-CimInstance Win32_Service -Filter (\"Name='\"+$name+\"'\");" - "Write-Output ('Service state after initialization: '+$service.State)" - "};" - "$since=(Get-Date).AddMinutes(-10);" - "Write-Output 'Recent Windows service events:';" - "Get-WinEvent -FilterHashtable @{LogName='System';StartTime=$since} -ErrorAction SilentlyContinue | " - "Where-Object {$_.ProviderName -eq 'Service Control Manager' -and $_.Message -match $name} | " - "Select-Object -First 8 TimeCreated,Id,LevelDisplayName,Message | Format-List | Out-String | Write-Output;" - "Write-Output 'Recent MySQL/Application events:';" - "Get-WinEvent -FilterHashtable @{LogName='Application';StartTime=$since} -ErrorAction SilentlyContinue | " - "Where-Object {$_.ProviderName -match 'MySQL|Application Error' -or $_.Message -match 'mysqld|MySQL80'} | " - "Select-Object -First 8 TimeCreated,ProviderName,Id,LevelDisplayName,Message | " - "Format-List | Out-String | Write-Output;" - "if($service.State -eq 'Running'){exit 0}else{exit 1}" + MYSQL_RECOVERY_HELPERS + + f"\n$name='{escaped_service_name}';\n" + + f"$mysqlExe='{escaped_mysql_executable}';\n" + + r''' +$ErrorActionPreference = 'Continue' +$data = '' +$recoveryFailed = $false +try { + $service = Get-CimInstance Win32_Service -Filter ("Name='"+$name+"'") -ErrorAction Stop + if (-not $service) { throw ('Windows service not found: ' + $name) } + $service | Select-Object Name,State,StartName,PathName,ExitCode,ProcessId | + Format-List | Out-String | Write-Output + if ($service.State -eq 'Running') { exit 0 } + $layout = Get-MySqlServiceLayout $service + $mysqlExe = $layout.Server + $ini = $layout.Ini + $data = $layout.Data + Write-Output ('mysqld.exe: ' + $mysqlExe) + Write-Output ('my.ini: ' + $ini) + Write-Output ('Data directory: ' + $data) + if ($service.State -eq 'Stopped' -and $service.ProcessId -eq 0) { + $initialized = Initialize-EmptyMySqlData $service $layout + # Use the account actually registered for this service. + $account = $service.StartName + if ($account -eq 'NT AUTHORITY\NetworkService') { $sid = 'S-1-5-20' } + elseif ($account -eq 'LocalSystem') { $sid = 'S-1-5-18' } + else { + $identity = New-Object Security.Principal.NTAccount($account) + $sid = $identity.Translate([Security.Principal.SecurityIdentifier]).Value + } + $grantCode = Invoke-MySqlRecoveryCommand 'icacls.exe' @( + $data, '/grant', ('*' + $sid + ':(OI)(CI)M'), '/T', '/C' + ) + if ($grantCode -ne 0) { throw ('Data directory permission repair failed: ' + $grantCode) } + Write-Output 'mysqld configuration validation:' + $validation = Invoke-MySqlRecoveryCommand $mysqlExe @( + ('--defaults-file=' + $ini), '--validate-config', '--console' + ) + if ($validation -ne 0) { throw ('MySQL configuration validation failed: ' + $validation) } + try { Start-Service -Name $name -ErrorAction Stop } + catch { Write-Output ('Start-Service: ' + $_.Exception.Message) } + } + for ($attempt = 0; $attempt -lt 12; $attempt++) { + $service = Get-CimInstance Win32_Service -Filter ("Name='"+$name+"'") + if ($service.State -eq 'Running') { + Write-Output 'MySQL service is running.' + exit 0 + } + if ($service.State -eq 'Stopped') { break } + Start-Sleep -Seconds 1 + } + Write-Output ('Service state after recovery: ' + $service.State) +} catch { + $recoveryFailed = $true + Write-Output ('MySQL recovery failed: ' + $_.Exception.Message) +} +if ($data -and (Test-Path -LiteralPath $data)) { + $err = Get-ChildItem -LiteralPath $data -Filter '*.err' -File -ErrorAction SilentlyContinue | + Sort-Object LastWriteTime -Descending | Select-Object -First 1 + if ($err) { + Write-Output ('Error log: ' + $err.FullName) + Get-Content -LiteralPath $err.FullName -Tail 30 + } else { Write-Output 'No .err log found with administrator privileges.' } +} +$since = (Get-Date).AddMinutes(-10) +if ($recoveryFailed) { exit 2 } +Write-Output 'Recent Windows service events:' +Get-WinEvent -FilterHashtable @{LogName='System';StartTime=$since} -ErrorAction SilentlyContinue | + Where-Object {$_.ProviderName -eq 'Service Control Manager' -and $_.Message -match $name} | + Select-Object -First 3 TimeCreated,Id,Message | Format-List | Out-String | Write-Output +exit 1 +''' ) diff --git a/deploy_tool/mysql_recovery.py b/deploy_tool/mysql_recovery.py new file mode 100644 index 0000000..1f5db3d --- /dev/null +++ b/deploy_tool/mysql_recovery.py @@ -0,0 +1,138 @@ +"""PowerShell helpers for recovering a fresh Windows MySQL installation. + +Embedded as Python text so the helpers ship inside the single-file executable. +Only an empty data directory is eligible for automatic initialization. +""" + +MYSQL_RECOVERY_HELPERS = r''' +function Read-MySqlOptionFile([string]$Path) { + $bytes = [IO.File]::ReadAllBytes($Path) + $encoding = New-Object Text.UTF8Encoding($false, $true) + if ($bytes.Length -ge 2 -and $bytes[0] -eq 255 -and $bytes[1] -eq 254) { + $encoding = [Text.Encoding]::Unicode + } elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 254 -and $bytes[1] -eq 255) { + $encoding = [Text.Encoding]::BigEndianUnicode + } elseif ($bytes.Length -ge 3 -and $bytes[0] -eq 239 -and $bytes[1] -eq 187 -and $bytes[2] -eq 191) { + $encoding = New-Object Text.UTF8Encoding($true, $true) + } + try { $text = $encoding.GetString($bytes).TrimStart([char]0xFEFF) } + catch { + $encoding = [Text.Encoding]::Default + $text = $encoding.GetString($bytes) + } + return @{ Text = $text; Encoding = $encoding } +} + +function Get-MySqlSettings([string]$Text, [string]$ServiceName) { + if ($Text -match '(?m)^\s*!include') { + throw 'Automatic recovery does not support included option files. Check my.ini and its includes.' + } + $settings = @{} + $section = '' + foreach ($line in ($Text -split '\r?\n')) { + if ($line -match '^\s*\[([^]]+)\]') { $section = $Matches[1]; continue } + if ($section -notin @('mysqld', 'server', 'mysqld-8.0', $ServiceName)) { continue } + if ($line -match '^\s*([\w-]+)\s*=\s*(.*?)\s*$') { + $key = $Matches[1].Replace('_', '-').ToLowerInvariant() + $value = $Matches[2] + if ($value -match '^"([^"]*)"\s*(?:[#;].*)?$' -or $value -match "^'([^']*)'\s*(?:[#;].*)?$") { + $value = $Matches[1] + } else { $value = ($value -replace '\s+[#;].*$', '').Trim() } + $settings[$key] = $value + } + } + return $settings +} + +function Get-MySqlServiceLayout($Service) { + $command = $Service.PathName + if ($command -notmatch '^\s*"([^"]+\\mysqld\.exe)"|^\s*(\S+\\mysqld\.exe)') { + throw 'Cannot resolve mysqld.exe from the Windows service command.' + } + $serverExe = if ($Matches[1]) { $Matches[1] } else { $Matches[2] } + if ($command -notmatch '(?i)--defaults-file(?:=|\s+)(?:"([^"]+)"|(\S+))') { + throw 'Cannot resolve --defaults-file from the Windows service command.' + } + $iniPath = if ($Matches[1]) { $Matches[1] } else { $Matches[2] } + if (-not [IO.Path]::IsPathRooted($iniPath) -or -not (Test-Path -LiteralPath $iniPath -PathType Leaf)) { + throw ('MySQL option file is missing or not absolute: ' + $iniPath) + } + $document = Read-MySqlOptionFile $iniPath + $settings = Get-MySqlSettings $document.Text $Service.Name + $dataPath = $settings['datadir'] + if (-not $dataPath -or -not [IO.Path]::IsPathRooted($dataPath)) { + throw 'my.ini must contain an absolute datadir for automatic recovery.' + } + return @{ Server = $serverExe; Ini = $iniPath; Data = [IO.Path]::GetFullPath($dataPath) } +} + +function Repair-MySqlLogNames([string]$Text, [string]$ServiceName) { + $names = @{ + 'log-bin' = 'mysql-bin'; 'log-bin-index' = 'mysql-bin.index' + 'log-error' = 'mysql-error.err'; 'pid-file' = 'mysql.pid' + 'general-log-file' = 'mysql-general.log'; 'slow-query-log-file' = 'mysql-slow.log' + 'relay-log' = 'mysql-relay-bin'; 'relay-log-index' = 'mysql-relay-bin.index' + } + $section = '' + $newline = if ($Text.Contains("`r`n")) { "`r`n" } else { "`n" } + $lines = foreach ($line in ($Text -split '\r?\n')) { + if ($line -match '^\s*\[([^]]+)\]') { $section = $Matches[1] } + if ($section -in @('mysqld', 'server', 'mysqld-8.0', $ServiceName) -and + $line -match '^\s*([\w-]+)(?:\s*=.*|\s*)$') { + $key = $Matches[1].Replace('_', '-').ToLowerInvariant() + if ($names.ContainsKey($key)) { $key + '=' + $names[$key]; continue } + } + $line + } + # Explicit names avoid host-name-derived paths, including on Chinese Windows. + return (($lines -join $newline).TrimEnd([char[]]"`r`n") + $newline + + '[mysqld]' + $newline + 'log-bin=mysql-bin' + $newline + + 'log-bin-index=mysql-bin.index' + $newline + 'log-error=mysql-error.err' + $newline) +} + +function Invoke-MySqlRecoveryCommand([string]$Executable, [string[]]$Arguments) { + # Convert native stderr records to plain messages instead of PowerShell stack traces. + & $Executable @Arguments 2>&1 | ForEach-Object { Write-Host $_.ToString() } + return $LASTEXITCODE +} + +function Initialize-EmptyMySqlData($Service, $Layout) { + if ($Service.State -ne 'Stopped' -or [int]$Service.ProcessId -ne 0) { + throw 'MySQL must be fully stopped before initialization.' + } + $dataPath = $Layout.Data + if (-not (Test-Path -LiteralPath $dataPath -PathType Container)) { + throw ('Data directory does not exist: ' + $dataPath) + } + if ((Get-Item -LiteralPath $dataPath -ErrorAction Stop).Attributes -band [IO.FileAttributes]::ReparsePoint) { + throw 'Automatic initialization is disabled for linked data directories.' + } + $entries = @(Get-ChildItem -LiteralPath $dataPath -Force -ErrorAction Stop) + if ($entries.Count -gt 0) { + Write-Host ('Data directory is not empty; initialization and log renaming skipped: ' + $dataPath) + Write-Host ('Existing entries: ' + (($entries | Select-Object -First 12 -ExpandProperty Name) -join ', ')) + return $false + } + $document = Read-MySqlOptionFile $Layout.Ini + $settings = Get-MySqlSettings $document.Text $Service.Name + # Changing an empty default directory must never initialize a different configured directory. + if ([IO.Path]::GetFullPath($settings['datadir']) -ne $dataPath) { throw 'datadir changed during recovery.' } + $backup = $Layout.Ini + '.autodeploy-' + [Guid]::NewGuid().ToString('N') + '.bak' + [IO.File]::Copy($Layout.Ini, $backup, $false) + Write-Host ('Configuration backup: ' + $backup) + $updated = Repair-MySqlLogNames $document.Text $Service.Name + [IO.File]::WriteAllText($Layout.Ini, $updated, $document.Encoding) + Write-Host 'Empty data directory: using ASCII log names and initializing MySQL.' + # Initialization precedes service startup. Disable binlog for this invocation only. + $code = Invoke-MySqlRecoveryCommand $Layout.Server @( + ('--defaults-file=' + $Layout.Ini), ('--datadir=' + $dataPath), + '--initialize-insecure', '--skip-log-bin', '--console' + ) + if ($code -ne 0) { throw ('MySQL initialization failed, exit code: ' + $code + '. Existing files were retained.') } + if (-not (Test-Path -LiteralPath (Join-Path $dataPath 'mysql.ibd'))) { + throw 'Initialization returned success but mysql.ibd is missing. Refusing to start the service.' + } + Write-Host 'MySQL data directory initialized successfully.' + return $true +} +''' diff --git a/tests/test_installer.py b/tests/test_installer.py index 4c8b702..91f9abb 100644 --- a/tests/test_installer.py +++ b/tests/test_installer.py @@ -434,6 +434,35 @@ class InstallerTests(unittest.TestCase): find_console.assert_not_called() self.assertTrue(any("跳过 Server 安装" in message for message in messages)) + def test_newly_initialized_mysql_sets_password_using_empty_root_password(self): + denied = CompletedProcess([], 1, stdout="", stderr="ERROR 1045: Access denied") + success = CompletedProcess([], 0, stdout="", stderr="") + with patch("deploy_tool.installer.subprocess.run", side_effect=[ + denied, denied, success, success, success, + ]) as run: + configure_mysql_weak_password_policy(Path("mysql.exe"), lambda _: None, attempts=1) + self.assertEqual(run.call_args_list[3].kwargs["env"]["MYSQL_PWD"], "") + self.assertIn("ALTER USER", run.call_args_list[3].kwargs["input"]) + self.assertEqual(run.call_args_list[4].kwargs["env"]["MYSQL_PWD"], MYSQL_ROOT_PASSWORD) + + def test_reports_initialization_cause_without_waiting_for_service_timeout(self): + with tempfile.TemporaryDirectory() as temporary: + log = Path(temporary) / "mysql-service-start.log" + log.write_text( + "[ERROR] binary log index not found\n" + "diagnostic detail\n" * 40 + + "MySQL recovery failed: MySQL initialization failed, exit code: 1\n", + encoding="utf-8", + ) + with ( + patch("deploy_tool.installer.query_mysql_service_state", return_value=1), + patch("deploy_tool.installer.default_mysql_service_log_path", return_value=log), + patch("deploy_tool.installer.run_mysql_service_start_diagnostic", return_value=2), + patch("deploy_tool.installer.time.sleep") as sleep, + ): + with self.assertRaisesRegex(RuntimeError, "binary log index not found"): + ensure_mysql_service_running(lambda _: None) + sleep.assert_not_called() + def test_reads_mysql_windows_service_state_without_console_window(self): running = CompletedProcess([], 0, stdout=b"STATE : 4 RUNNING\r\n", stderr=b"") @@ -505,7 +534,7 @@ class InstallerTests(unittest.TestCase): self.assertIn("--validate-config", script) self.assertIn("initialize-insecure", script) self.assertIn("S-1-5-20", script) - self.assertIn("Service state after permission repair", script) + self.assertIn("Service state after recovery", script) self.assertIn("Get-WinEvent", script) self.assertIn("mysqld.exe", script) diff --git a/tests/test_mysql_recovery.py b/tests/test_mysql_recovery.py new file mode 100644 index 0000000..ecb45c6 --- /dev/null +++ b/tests/test_mysql_recovery.py @@ -0,0 +1,272 @@ +"""Execute recovery helpers against temporary files and mocked native commands. + +No MySQL installation or Windows service is used by these tests. +""" + +import json +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +from deploy_tool.installer import build_mysql_service_start_script +from deploy_tool.mysql_recovery import MYSQL_RECOVERY_HELPERS + + +POWERSHELL = shutil.which("powershell.exe") + + +def ps_literal(value): + return "'" + str(value).replace("'", "''") + "'" + + +@unittest.skipUnless(POWERSHELL, "Windows PowerShell is required") +class MySqlRecoveryTests(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory(prefix="autodeploy-recovery-test-") + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name) + self.data = self.root / "实际数据目录" + self.data.mkdir() + self.ini = self.root / "自定义配置.ini" + self.server = self.root / "Server Files" / "bin" / "mysqld.exe" + self.config = ( + "# Preserve this comment\r\n" + "[client]\r\n" + "log-error=client-only.err\r\n" + "port=3307\r\n" + "[mysqld]\r\n" + f'datadir="{self.data.as_posix()}"\r\n' + 'log_bin="马航宇-bin"\r\n' + 'log-bin-index="乱码?-bin.index"\r\n' + 'log-error="马航宇.err"\r\n' + "max_connections=151\r\n" + ) + self.ini.write_bytes(self.config.encode("utf-8")) + + def run_harness(self, body, *, state="Stopped", process_id=0, exit_code=0, + create_dictionary=True): + result_path = self.root / "result.json" + script_path = self.root / "exercise-recovery.ps1" + command_line = f'"{self.server}" --defaults-file="{self.ini}" MySQL80' + mock = r''' +$script:commandCalls = @() +function Invoke-MySqlRecoveryCommand([string]$Executable, [string[]]$Arguments) { + $script:commandCalls += @{ Executable = $Executable; Arguments = @($Arguments) } + if ($script:createDictionary -and $script:mockExitCode -eq 0) { + [IO.File]::WriteAllText((Join-Path $script:testData 'mysql.ibd'), 'test dictionary') + } + return $script:mockExitCode +} +''' + script = ( + "$ErrorActionPreference = 'Stop'\n" + + MYSQL_RECOVERY_HELPERS + + f"\n$script:testData = {ps_literal(self.data)}\n" + + f"$script:mockExitCode = {exit_code}\n" + + "$script:createDictionary = " + + ("$true" if create_dictionary else "$false") + + "\n" + + mock + + "$service = [pscustomobject]@{ Name = 'MySQL80'; " + + f"PathName = {ps_literal(command_line)}; " + + f"State = {ps_literal(state)}; ProcessId = {process_id} }}\n" + + "$layout = Get-MySqlServiceLayout $service\n" + + body + + f"\n$json = $result | ConvertTo-Json -Depth 10 -Compress\n" + + f"[IO.File]::WriteAllText({ps_literal(result_path)}, $json, " + + "(New-Object Text.UTF8Encoding($false)))\n" + ) + script_path.write_text(script, encoding="utf-8-sig") + completed = subprocess.run( + [POWERSHELL, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", + "-File", str(script_path)], + capture_output=True, + timeout=20, + creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0), + ) + self.assertEqual( + completed.returncode, 0, + (completed.stdout + completed.stderr).decode("utf-8", errors="replace"), + ) + self.assertTrue(result_path.exists(), "PowerShell harness produced no result") + return json.loads(result_path.read_text(encoding="utf-8-sig")) + + def test_resolves_actual_service_executable_config_and_data_paths(self): + result = self.run_harness("$result = $layout") + self.assertEqual(Path(result["Server"]), self.server) + self.assertEqual(Path(result["Ini"]), self.ini) + self.assertEqual(Path(result["Data"]), self.data) + + def test_empty_data_recovery_backs_up_and_normalizes_chinese_log_names(self): + original = self.ini.read_bytes() + result = self.run_harness(""" +$initialized = Initialize-EmptyMySqlData $service $layout +$result = @{ Initialized = $initialized; Calls = @($script:commandCalls) } +""") + self.assertTrue(result["Initialized"]) + backups = list(self.root.glob(self.ini.name + ".autodeploy-*.bak")) + self.assertEqual(len(backups), 1) + self.assertEqual(backups[0].read_bytes(), original) + updated = self.ini.read_text(encoding="utf-8") + self.assertIn("log-bin=mysql-bin", updated) + self.assertIn("log-bin-index=mysql-bin.index", updated) + self.assertIn("log-error=mysql-error.err", updated) + self.assertNotIn("马航宇-bin", updated) + self.assertNotIn("乱码?-bin.index", updated) + self.assertIn("# Preserve this comment", updated) + self.assertIn("log-error=client-only.err", updated) + self.assertIn("max_connections=151", updated) + self.assertIn(self.data.as_posix(), updated) + self.assertIn(b"\r\n", self.ini.read_bytes()) + self.assertEqual(len(result["Calls"]), 1) + call = result["Calls"][0] + self.assertEqual(Path(call["Executable"]), self.server) + self.assertEqual(call["Arguments"][0], "--defaults-file=" + str(self.ini)) + self.assertIn("--initialize-insecure", call["Arguments"]) + self.assertIn("--skip-log-bin", call["Arguments"]) + + def test_recovery_preserves_utf8_bom_and_utf16_configuration(self): + for encoding, preamble in (("utf-8-sig", b"\xef\xbb\xbf"), + ("utf-16", b"\xff\xfe")): + with self.subTest(encoding=encoding): + self.ini.write_bytes(self.config.encode(encoding)) + result = self.run_harness(""" +$result = @{ Initialized = (Initialize-EmptyMySqlData $service $layout) } +""") + self.assertTrue(result["Initialized"]) + content = self.ini.read_bytes() + self.assertTrue(content.startswith(preamble)) + self.assertIn(self.data.as_posix(), content.decode(encoding)) + (self.data / "mysql.ibd").unlink() + + def test_existing_dictionary_or_user_data_blocks_all_initialization_changes(self): + for relative in ("mysql.ibd", "ibdata1", "business/orders.ibd", "mysql-bin.index"): + with self.subTest(existing=relative): + existing = self.data / relative + existing.parent.mkdir(exist_ok=True) + existing.write_bytes(b"existing data must be preserved") + original = self.ini.read_bytes() + result = self.run_harness(""" +$initialized = Initialize-EmptyMySqlData $service $layout +$result = @{ Initialized = $initialized; Calls = @($script:commandCalls) } +""") + self.assertFalse(result["Initialized"]) + self.assertEqual(result["Calls"], []) + self.assertEqual(self.ini.read_bytes(), original) + self.assertEqual(existing.read_bytes(), b"existing data must be preserved") + self.assertEqual(list(self.root.glob(self.ini.name + ".autodeploy-*.bak")), []) + existing.unlink() + if existing.parent != self.data: + existing.parent.rmdir() + + def test_pending_service_blocks_configuration_changes_and_initialization(self): + original = self.ini.read_bytes() + result = self.run_harness(""" +$failure = '' +try { Initialize-EmptyMySqlData $service $layout | Out-Null } +catch { $failure = $_.Exception.Message } +$result = @{ Failure = $failure; Calls = @($script:commandCalls) } +""", state="Start Pending", process_id=1234) + self.assertIn("fully stopped", result["Failure"]) + self.assertEqual(result["Calls"], []) + self.assertEqual(self.ini.read_bytes(), original) + self.assertEqual(list(self.data.iterdir()), []) + self.assertEqual(list(self.root.glob(self.ini.name + ".autodeploy-*.bak")), []) + + def test_nonzero_initialize_exit_is_surfaced(self): + result = self.run_harness(""" +$failure = '' +try { Initialize-EmptyMySqlData $service $layout | Out-Null } +catch { $failure = $_.Exception.Message } +$result = @{ Failure = $failure; Calls = @($script:commandCalls) } +""", exit_code=7) + self.assertIn("exit code: 7", result["Failure"]) + self.assertEqual(len(result["Calls"]), 1) + self.assertEqual(list(self.data.iterdir()), []) + self.assertEqual(len(list(self.root.glob(self.ini.name + ".autodeploy-*.bak"))), 1) + + def test_initialize_success_without_dictionary_is_rejected(self): + result = self.run_harness(""" +$failure = '' +try { Initialize-EmptyMySqlData $service $layout | Out-Null } +catch { $failure = $_.Exception.Message } +$result = @{ Failure = $failure; Calls = @($script:commandCalls) } +""", create_dictionary=False) + self.assertIn("mysql.ibd is missing", result["Failure"]) + self.assertEqual(len(result["Calls"]), 1) + + def run_generated_script(self, initialize_exit): + events_path = self.root / "generated-script-events.json" + command_line = f'"{self.server}" --defaults-file="{self.ini}" MySQL80' + mocks = ( + f"\n$script:eventFile = {ps_literal(events_path)}\n" + f"$script:testData = {ps_literal(self.data)}\n" + f"$script:initExit = {initialize_exit}\n" + "$script:mockService = [pscustomobject]@{ Name = 'MySQL80'; " + f"PathName = {ps_literal(command_line)}; " + "State = 'Stopped'; ProcessId = 0; StartName = 'NT AUTHORITY\\NetworkService' }\n" + + r''' +$script:events = @() +function Record-RecoveryEvent([string]$Name) { + $script:events += $Name + $json = ConvertTo-Json -InputObject @($script:events) -Compress + [IO.File]::WriteAllText($script:eventFile, $json, (New-Object Text.UTF8Encoding($false))) +} +function Get-CimInstance { return $script:mockService } +function Get-WinEvent {} +function Start-Sleep {} +function Start-Service { + Record-RecoveryEvent 'start' + $script:mockService.State = 'Running' + $script:mockService.ProcessId = 100 +} +function Invoke-MySqlRecoveryCommand([string]$Executable, [string[]]$Arguments) { + if ($Arguments -contains '--initialize-insecure') { + Record-RecoveryEvent 'initialize' + if ($script:initExit -eq 0) { + [IO.File]::WriteAllText((Join-Path $script:testData 'mysql.ibd'), 'test dictionary') + } + return $script:initExit + } + if ($Executable -eq 'icacls.exe') { Record-RecoveryEvent 'acl'; return 0 } + if ($Arguments -contains '--validate-config') { Record-RecoveryEvent 'validate'; return 0 } + throw 'Unexpected native operation in test harness' +} +''' + ) + generated = build_mysql_service_start_script("MySQL80", self.server) + # Replace only the helper prefix to override native/service commands before + # exercising the complete generated workflow, including its process exit. + self.assertTrue(generated.startswith(MYSQL_RECOVERY_HELPERS)) + instrumented = ( + MYSQL_RECOVERY_HELPERS + mocks + generated[len(MYSQL_RECOVERY_HELPERS):] + ) + script_path = self.root / "generated-start-script.ps1" + script_path.write_text(instrumented, encoding="utf-8-sig") + completed = subprocess.run( + [POWERSHELL, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", + "-File", str(script_path)], + capture_output=True, + timeout=20, + creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0), + ) + self.assertTrue(events_path.exists(), (completed.stdout + completed.stderr)) + events = json.loads(events_path.read_text(encoding="utf-8")) + return completed, events + + def test_generated_script_never_starts_service_after_initialization_failure(self): + completed, events = self.run_generated_script(initialize_exit=7) + self.assertEqual(completed.returncode, 2) + self.assertEqual(events, ["initialize"]) + self.assertIn(b"MySQL initialization failed, exit code: 7", completed.stdout) + + def test_generated_script_initializes_before_granting_validating_and_starting(self): + completed, events = self.run_generated_script(initialize_exit=0) + self.assertEqual(completed.returncode, 0, completed.stdout + completed.stderr) + self.assertEqual(events, ["initialize", "acl", "validate", "start"]) + + +if __name__ == "__main__": + unittest.main()