fix(部署): 修复 install-service.ps1 密码解析崩溃(Trim 参数类型错误)并加 trap 友好报错
2个文件已修改
84 ■■■■■ 已修改文件
HANDOFF.md 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
deploy-templates/backend/install-service.ps1 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
HANDOFF.md
@@ -52,6 +52,14 @@
  - 新包:`deploy/traffic-audit-deploy-20261003-4/`(394 文件 / 110.50 MB,zip 83.78 MB)——**当前唯一可用包**。
- **发布包清理(10-03 晚,用户确认“只留 -4”)**:已删除 `traffic-audit-release-20261002/`(+zip)、`traffic-audit-deploy-20261003/`(+zip)、`-2/`(+zip)、`-3/`(+zip) 及空的 `server-fix/`,`deploy\` 现在只有 `traffic-audit-deploy-20261003-4/` 和它的 zip(释放约 671MB)。
  > 因此本文件上面提到的 `deploy/traffic-audit-deploy-20261003/`(388 文件)与 `deploy/traffic-audit-release-20261002/` 均为**历史记录**,磁盘上已不存在,别再去找。
- **紧急修复(10-03 晚):`install-service.ps1` 密码解析崩溃,服务根本没装上**
  - 现象:用户在服务器(`D:\trafficAudit`)双击 `install-service.cmd`,报 `无法将“Trim”的参数“trimChars”转换为类型“System.Char”`,位置 `install-service.ps1:210`。
  - 根因:那行是 `.Trim(' ''', '"')`,其中 `' '''` 被 PowerShell 解析成一个**长度 2 的字符串**(空格 + 单引号),`Trim(char[])` 无法把 2 字符串转成单个 char → 整行抛异常(`MethodArgumentConversionInvalidCastArgument`)。
  - 修复:把这段抽成纯函数 `Test-AppConfigPassword`,改写为 `Trim()` → `Trim("'")` → `Trim('"')`;并加脚本级 `trap`,任何异常只打印一行说明 + 行号,不再甩红字堆栈。
  - 验证:8 个样本(缺文件 / 空值 / 中文占位符 / 裸值 / 双引号 / 单引号 / 含空格特殊字符 / 真实模板文件)全部符合预期;打包内脚本语法解析通过、`-Status` 实跑正常、非管理员时 trap 输出干净错误并 exit 1。
  - 教训:这条分支之前没被任何测试覆盖(`-Status` 提前 exit、非管理员在 `Assert-Admin` 就退出),所以解析类逻辑要抽成纯函数并用样本测。
  - 包名:修复版重打包时脚本复用了已释放的基础名,最终已删掉旧坏包并改名,`deploy\` 仍然是**唯一**的 `traffic-audit-deploy-20261003-4\`(含修复后的 install-service.ps1)与同名 zip。
  - 用户侧最小修复:只需用新包里的 `backend\install-service.ps1` 覆盖服务器 `D:\trafficAudit\install-service.ps1`,再重跑 `install-service.cmd`(不必重拷 110MB)。
## 最新(2026-10-02)
deploy-templates/backend/install-service.ps1
@@ -45,6 +45,21 @@
$appConfig = Join-Path $root 'application.yml'
$serviceId = 'TrafficAuditBackend'
# Any terminating error below lands here instead of dumping a raw stack trace
# at the operator. The line number is kept so a copy of this window is enough
# to diagnose a report.
trap {
  Write-Host ''
  Write-Host '=============================================================='
  Write-Host ' ERROR: the installer stopped before finishing.'
  Write-Host ('        ' + $_.Exception.Message)
  Write-Host ('        at line ' + $_.InvocationInfo.ScriptLineNumber + ' of install-service.ps1')
  Write-Host ' The service may not be installed. Send this window text to the'
  Write-Host ' developer if the reason is not obvious.'
  Write-Host '=============================================================='
  exit 1
}
function Assert-Admin {
  $identity  = [Security.Principal.WindowsIdentity]::GetCurrent()
  $principal = New-Object Security.Principal.WindowsPrincipal($identity)
@@ -146,6 +161,45 @@
  }
}
function Test-AppConfigPassword {
  param([string]$Path)
  # Returns $true when the datasource password looks usable. Prints a WARN and
  # returns $false for the states that break startup with
  # "Access denied ... (using password: NO)": missing file / empty value / placeholder.
  if (-not (Test-Path -LiteralPath $Path)) {
    Write-Host 'WARN: application.yml is missing next to the jar.'
    Write-Host '      The built-in password placeholder resolves to an empty string and startup will fail with'
    Write-Host "      Java SQLException: Access denied for user 'root'@'localhost' (using password: NO)."
    return $false
  }
  $line = Select-String -LiteralPath $Path -Pattern '^\s*password\s*:' | Select-Object -First 1
  if (-not $line) {
    Write-Host "WARN: no 'password:' entry found in application.yml; check the datasource block."
    return $false
  }
  # Trim() takes a single char, so strip whitespace, then one quote kind at a
  # time. Passing a 2-char string here throws
  # "cannot convert value ... to type System.Char" and aborts the script.
  $value = $line.Line -replace '^\s*password\s*:\s*', ''
  $value = $value.Trim()
  $value = $value.Trim("'")
  $value = $value.Trim('"')
  if ($value -eq '') {
    Write-Host "WARN: spring.datasource.password is empty in application.yml; startup may fail with '(using password: NO)'."
    return $false
  }
  if ($value -match '[^\x00-\x7F]') {
    Write-Host 'WARN: the datasource password contains non-ASCII characters.'
    Write-Host '      If it is still the Chinese placeholder text, replace it with the real MySQL password first.'
    return $false
  }
  return $true
}
# ---------- read-only status: no elevation needed ----------
if ($Status) {
  $service = Get-ServiceState
@@ -197,27 +251,7 @@
if (-not (Test-Path $wrapper)) {
  throw "Missing wrapper executable: $wrapper`nRebuild the deploy package with pack-deploy.ps1 (it provides WinSW), or copy WinSW-x64.exe here renamed to trafficAudit.exe."
}
$passwordOk = $false
if (-not (Test-Path $appConfig)) {
  Write-Host "WARN: application.yml is missing next to the jar."
  Write-Host "      The built-in password placeholder resolves to an empty string and startup will fail with"
  Write-Host "      Java SQLException: Access denied for user 'root'@'localhost' (using password: NO)."
} else {
  $line = Select-String -LiteralPath $appConfig -Pattern '^\s*password\s*:' | Select-Object -First 1
  if (-not $line) {
    Write-Host "WARN: no 'password:' entry found in application.yml; check the datasource block."
  } else {
    $value = ($line.Line -replace '^\s*password\s*:\s*', '').Trim(' ''', '"')
    if ($value -eq '') {
      Write-Host "WARN: spring.datasource.password is empty in application.yml; startup may fail with '(using password: NO)'."
    } elseif ($value -match '[^\x00-\x7F]') {
      Write-Host "WARN: the datasource password contains non-ASCII characters."
      Write-Host "      If it is still the Chinese placeholder text, replace it with the real MySQL password first."
    } else {
      $passwordOk = $true
    }
  }
}
$passwordOk = Test-AppConfigPassword -Path $appConfig
$java = Resolve-JavaPath -Explicit $JavaPath
Set-WrapperJavaPath -JavaExe $java