Files
syops/scripts/rpa/set_rpa_worker_env.ps1
T

163 lines
6.1 KiB
PowerShell

<#
.SYNOPSIS
为货代库存 RPA Worker 写入 Windows 用户级永久环境变量(含共享 scope 与心跳)。
.DESCRIPTION
test/prod 部署时必须配置 SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID,与 Ubuntu 后端
SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID 一致,否则 Worker 不会写哨兵文件,
后端就绪检查将报 shared_sentinel_missing。
RPA Profile 凭据解密还要求 Windows Worker 与对应环境后端使用同一个
SHIPPING_NOTICE_RPA_CREDENTIAL_KEY。脚本通过 SecureString 交互读取该密钥,
不接收明文命令行参数,也不会在控制台显示密钥。
用法(以管理员或 RPA 专用账号登录后执行一次):
.\scripts\rpa\set_rpa_worker_env.ps1 -Environment test -ApiBase "http://192.168.1.20:8890" -ToolApiKey "<密钥>"
.\scripts\rpa\set_rpa_worker_env.ps1 -Environment prod -ApiBase "http://生产地址:端口" -ToolApiKey "<密钥>"
执行后请关闭并重新打开 PowerShell,再运行 freight_rpa_worker.py。
完整说明见 docs/features/shipping-notice/technical.md §1.5.5a / §1.5.10。
#>
param(
[Parameter(Mandatory = $true)]
[ValidateSet("test", "prod")]
[string]$Environment,
[Parameter(Mandatory = $true)]
[string]$ApiBase,
[Parameter(Mandatory = $true)]
[string]$ToolApiKey,
[SecureString]$CredentialKey,
[string]$DsvUsername = "",
[string]$DsvPassword = "",
[string]$CraneUsername = "",
[string]$CranePassword = ""
)
$ErrorActionPreference = "Stop"
function ConvertTo-PlainText {
param([Parameter(Mandatory = $true)][SecureString]$Value)
$pointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Value)
try {
return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($pointer)
} finally {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($pointer)
}
}
if (-not $CredentialKey) {
$CredentialKey = Read-Host `
"请输入与 $Environment 后端一致的 SHIPPING_NOTICE_RPA_CREDENTIAL_KEY" `
-AsSecureString
}
$CredentialKeyPlainText = ConvertTo-PlainText -Value $CredentialKey
try {
$credentialKeyBytes = [Convert]::FromBase64String(
$CredentialKeyPlainText.Replace('-', '+').Replace('_', '/')
)
} catch {
throw "SHIPPING_NOTICE_RPA_CREDENTIAL_KEY 必须是有效的 Fernet URL-safe Base64 密钥"
}
if ($credentialKeyBytes.Length -ne 32) {
throw "SHIPPING_NOTICE_RPA_CREDENTIAL_KEY 解码后必须为 32 字节"
}
if ($Environment -eq "test") {
$Root = "C:\HxopsRpaTest"
$ScopeId = "hxops-rpa-test-data"
} else {
$Root = "C:\HxopsRpaProd"
$ScopeId = "hxops-rpa-prod-data"
}
$DataRoot = Join-Path $Root "data"
$Vars = [ordered]@{
"HXOPS_API_BASE" = $ApiBase
"SHIPPING_NOTICE_TOOL_API_KEY" = $ToolApiKey
"SHIPPING_NOTICE_RPA_CREDENTIAL_KEY" = $CredentialKeyPlainText
"SHIPPING_NOTICE_RPA_WORKER_ENVIRONMENT" = $Environment
"SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID" = $ScopeId
"SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_SECONDS" = "20"
"SHIPPING_NOTICE_RPA_WORKER_MOCK" = "false"
"SHIPPING_NOTICE_RPA_WORKER_HEADLESS" = "false"
"SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL" = "msedge"
"SHIPPING_NOTICE_RPA_WORKER_POLL_SECONDS" = "3"
"SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR" = Join-Path $DataRoot "inventory_triggers"
"SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR" = Join-Path $DataRoot "inventory_drop"
"SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR" = Join-Path $DataRoot "upload_triggers"
"SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR" = Join-Path $DataRoot "upload_queue"
# 上传站点配置通过受控环境变量单独注入;部署脚本永远不默认开启浏览器提交。
"SHIPPING_NOTICE_RPA_WORKER_UPLOAD_ENABLED" = "false"
"SHIPPING_NOTICE_RPA_WORKER_UPLOAD_CONFIRM_SUBMIT" = "false"
"SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR" = Join-Path $DataRoot "browser_profile"
"SHIPPING_NOTICE_RPA_WORKER_LOG_FILE" = Join-Path $DataRoot "logs\worker.log"
"SHIPPING_NOTICE_RPA_WORKER_LOG_MAX_BYTES" = "10485760"
"SHIPPING_NOTICE_RPA_WORKER_LOG_BACKUP_COUNT" = "5"
}
if ($DsvUsername) { $Vars["SHIPPING_NOTICE_RPA_WORKER_DSV_USERNAME"] = $DsvUsername }
if ($DsvPassword) { $Vars["SHIPPING_NOTICE_RPA_WORKER_DSV_PASSWORD"] = $DsvPassword }
if ($CraneUsername) { $Vars["SHIPPING_NOTICE_RPA_WORKER_CRANE_USERNAME"] = $CraneUsername }
if ($CranePassword) { $Vars["SHIPPING_NOTICE_RPA_WORKER_CRANE_PASSWORD"] = $CranePassword }
$SensitiveKeys = @(
"SHIPPING_NOTICE_TOOL_API_KEY",
"SHIPPING_NOTICE_RPA_CREDENTIAL_KEY",
"SHIPPING_NOTICE_RPA_WORKER_DSV_PASSWORD",
"SHIPPING_NOTICE_RPA_WORKER_CRANE_PASSWORD"
)
$UsernameKeys = @(
"SHIPPING_NOTICE_RPA_WORKER_DSV_USERNAME",
"SHIPPING_NOTICE_RPA_WORKER_CRANE_USERNAME"
)
function Get-MaskedUsername {
param([string]$Value)
if (-not $Value) { return $Value }
if ($Value -match '^([^@]+)@(.+)$') {
$local = $Matches[1]
$domain = $Matches[2]
if ($local.Length -le 3) {
return "***@$domain"
}
return ($local.Substring(0, 3) + "***@" + $domain)
}
if ($Value.Length -le 3) {
return "***"
}
return ($Value.Substring(0, 3) + "***")
}
function Get-DisplayValue {
param(
[string]$Key,
[string]$Value
)
if ($SensitiveKeys -contains $Key -and $Value) {
return "******"
}
if ($UsernameKeys -contains $Key -and $Value) {
return Get-MaskedUsername -Value $Value
}
return $Value
}
foreach ($entry in $Vars.GetEnumerator()) {
[Environment]::SetEnvironmentVariable($entry.Key, [string]$entry.Value, "User")
$display = Get-DisplayValue -Key $entry.Key -Value ([string]$entry.Value)
Write-Host ("[ok] {0}={1}" -f $entry.Key, $display)
}
$CredentialKeyPlainText = $null
Write-Host ""
Write-Host "Environment=$Environment scope_id=$ScopeId root=$Root"
Write-Host "请关闭并重新打开 PowerShell,然后验证:"
Write-Host " echo `$env:SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID"
Write-Host " cd $Root"
Write-Host " python scripts\rpa\freight_rpa_worker.py"