Files

70 lines
4.5 KiB
PowerShell
Raw Permalink Normal View History

2026-10-20 08:34:12 +08:00
[CmdletBinding()]
param(
[Parameter(Mandatory)][ValidateSet('coordinator', 'gateway', 'worker')][string]$Role,
[Parameter(Mandatory)][string]$InstallRoot,
[Parameter(Mandatory)][string]$CredentialFile,
[string]$JarPath,
[int]$Port = 0,
[int]$Capacity = 2,
[int]$UdpPort = 19002,
[int]$ImagePort = 0,
[string]$NodeName = '',
[string]$AdvertisedHost = '',
[string]$DiscoveryAddress = '',
[string]$CoordinatorUrl = '',
[switch]$CoordinatorOnly
)
. (Join-Path $PSScriptRoot 'Common.ps1')
if ($CoordinatorOnly -and $Role -ne 'coordinator') { throw 'CoordinatorOnly 只能用于 coordinator 角色。' }
if ($Capacity -lt 0 -or $Capacity -gt 64) { throw 'Capacity 必须在 0 到 64 之间。' }
if ($UdpPort -lt 1025 -or $UdpPort -gt 65535) { throw 'UdpPort 必须在 1025 到 65535 之间。' }
if (-not $Port) { $Port = @{coordinator = 18100; gateway = 18110; worker = 18120}[$Role] }
if ($Port -lt 1025 -or $Port -gt 65535) { throw 'Port 必须在 1025 到 65535 之间。' }
if ($Role -ne 'gateway' -and -not $CoordinatorOnly) {
# 图片直达执行节点;同机多实例采用不同入口,避免争抢 PXHJ 原有 6020。
if (-not $ImagePort) { $ImagePort = $Port + 1000 }
if ($ImagePort -lt 1025 -or $ImagePort -gt 65535) { throw 'ImagePort 必须在 1025 到 65535 之间。' }
}
foreach ($value in @($NodeName, $AdvertisedHost, $DiscoveryAddress, $CoordinatorUrl)) {
if ($value -match '[\r\n]') { throw '配置值不能包含换行。' }
}
if ($CoordinatorUrl -and $CoordinatorUrl -notmatch '^https?://[^\s/?#]+/?$') { throw 'CoordinatorUrl 必须为服务根地址。' }
if (-not $JarPath) { $JarPath = Join-Path (Split-Path -Parent $PSScriptRoot) 'target/xtrh-services-1.0.0.jar' }
if (-not (Test-Path -LiteralPath $JarPath)) { throw '未找到构建产物,请先运行 Build-Xtrh.ps1。' }
if (-not (Test-Path -LiteralPath $CredentialFile)) { throw '未找到私有集群凭证文件。' }
$root = [System.IO.Path]::GetFullPath($InstallRoot)
$processFile = Join-Path $root 'service-process.json'
if (Test-Path -LiteralPath $processFile) {
$record = Get-Content -LiteralPath $processFile -Raw | ConvertFrom-Json
if (Get-XtrhOwnedProcess -InstallRoot $root -Record $record) { throw '部署实例正在运行,请先运行 Stop-Xtrh.ps1。' }
}
$credentialContent = Get-Content -LiteralPath $CredentialFile -Raw
if ($credentialContent -notmatch '(?m)^fusion\.token=.{24,}$' -or $credentialContent -notmatch '(?m)^fusion\.cluster-id=[a-zA-Z0-9_-]{1,64}\r?$') {
throw '集群凭证文件内容不符合格式。'
}
foreach ($folder in @($root, (Join-Path $root 'config'), (Join-Path $root 'logs'), (Join-Path $root 'data'), (Join-Path $root 'scripts'))) {
[System.IO.Directory]::CreateDirectory($folder) | Out-Null
}
Copy-Item -LiteralPath $JarPath -Destination (Join-Path $root 'xtrh-services.jar') -Force
$privateFile = Join-Path $root 'config/cluster.local.properties'
Write-XtrhText -Path $privateFile -Content $credentialContent
Protect-XtrhPrivateFile -Path $privateFile
$lines = @('server.address=0.0.0.0', "server.port=$Port", "fusion.role=$Role", 'fusion.data-dir=./data',
"fusion.capacity=$Capacity", "fusion.gateway-port=$UdpPort", 'fusion.discovery-enabled=true')
if ($Role -eq 'coordinator') {
# 总控默认在同一个进程中执行任务;只调度模式需显式关闭执行能力。
$lines += ('fusion.coordinator-worker-enabled=' + (-not $CoordinatorOnly).ToString().ToLowerInvariant())
}
if ($Role -ne 'gateway' -and -not $CoordinatorOnly) { $lines += "fusion.image-port=$ImagePort" }
if ($NodeName) { $lines += ('fusion.node-name=' + (ConvertTo-XtrhPropertyValue -Value $NodeName)) }
if ($AdvertisedHost) { $lines += "fusion.advertised-host=$AdvertisedHost" }
if ($DiscoveryAddress) { $lines += "fusion.discovery-address=$DiscoveryAddress" }
# 默认没有固定总控地址,节点在局域网自动发现;跨网段时才使用统一域名兜底。
if ($CoordinatorUrl) { $lines += "fusion.coordinator-url=$CoordinatorUrl" }
Write-XtrhText -Path (Join-Path $root 'config/service.local.properties') -Content (($lines -join "`n") + "`n")
Write-XtrhText -Path (Join-Path $root 'deployment.json') -Content (@{role = $Role; port = $Port} | ConvertTo-Json)
foreach ($name in @('Common.ps1', 'Start-Xtrh.ps1', 'Stop-Xtrh.ps1', 'Register-XtrhStartup.ps1')) {
Copy-Item -LiteralPath (Join-Path $PSScriptRoot $name) -Destination (Join-Path $root "scripts/$name") -Force
}
Write-Output ('部署完成:' + $root + ',角色:' + $Role + ',HTTP 端口:' + $Port)