优化
This commit is contained in:
@@ -1,68 +1,27 @@
|
||||
param(
|
||||
[ValidateSet('local', 'dev', 'development', 'test', 'production')]
|
||||
[string]$Environment,
|
||||
[ValidateSet('cn', 'overseas')]
|
||||
[string]$Region = 'cn',
|
||||
[string]$ServerAddr = '127.0.0.1:8848',
|
||||
[ValidateSet('test')][string]$Environment = 'test',
|
||||
[Parameter(Mandatory)][string]$ServerAddr,
|
||||
[string]$Group = 'BAIYUE',
|
||||
[string]$Namespace
|
||||
[Parameter(Mandatory)][string]$Namespace,
|
||||
[string]$Username = $env:NACOS_USERNAME,
|
||||
[string]$Password = $env:NACOS_PASSWORD,
|
||||
[switch]$DryRun
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
if ([string]::IsNullOrWhiteSpace($Namespace) -or $Namespace -eq 'public') { throw 'Supply the actual TEST Namespace ID, not public or a display name' }
|
||||
$projectRoot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
|
||||
|
||||
if ($Environment -eq 'local') {
|
||||
if ($Namespace -and $Namespace -ne 'public') {
|
||||
throw 'local 环境只能发布到 public namespace'
|
||||
}
|
||||
$Namespace = 'public'
|
||||
$configProfile = 'local'
|
||||
} else {
|
||||
if (-not $Namespace -or $Namespace -eq 'public') {
|
||||
throw "$Environment/$Region 必须显式指定非 public 的 Nacos namespace"
|
||||
}
|
||||
$configProfile = $Region
|
||||
}
|
||||
|
||||
$configRoot = (Resolve-Path (Join-Path $projectRoot "config\nacos\$configProfile")).Path
|
||||
$healthUrl = "http://$ServerAddr/nacos/v1/console/health/readiness"
|
||||
$publishUrl = "http://$ServerAddr/nacos/v1/cs/configs"
|
||||
$health = Invoke-WebRequest -UseBasicParsing -Uri $healthUrl -TimeoutSec 5
|
||||
if ($health.StatusCode -ne 200 -or $health.Content.Trim() -ne 'OK') {
|
||||
throw "Nacos not ready: $healthUrl"
|
||||
}
|
||||
|
||||
$files = Get-ChildItem -LiteralPath $configRoot -File -Filter '*.yml' | Sort-Object Name
|
||||
if ($files.Count -eq 0) {
|
||||
throw "No Nacos configuration found: $configRoot"
|
||||
}
|
||||
|
||||
$published = 0
|
||||
$configRoot = Join-Path $projectRoot 'config/nacos/test'
|
||||
$files = Get-ChildItem -LiteralPath $configRoot -File -Filter '*.yml' | Sort-Object @{Expression={if ($_.Name -eq 'baiyue-common.yml') {0} else {1}}}, Name
|
||||
if ($files.Count -ne 14) { throw 'Expected common configuration and 13 retained services' }
|
||||
if ($DryRun) { $files | ForEach-Object { Write-Host "Would publish $($_.Name) -> $Namespace / $Group" }; return }
|
||||
if (-not $Username -or -not $Password) { throw 'NACOS_USERNAME and NACOS_PASSWORD are required' }
|
||||
$base = "http://$ServerAddr/nacos"
|
||||
$login = Invoke-RestMethod -Method Post -Uri "$base/v1/auth/login" -Body @{username=$Username; password=$Password} -TimeoutSec 10
|
||||
if (-not $login.accessToken) { throw 'Nacos authentication failed' }
|
||||
foreach ($file in $files) {
|
||||
$resolvedFile = (Resolve-Path -LiteralPath $file.FullName).Path
|
||||
if (-not $resolvedFile.StartsWith($configRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
|
||||
throw "Unexpected config path: $resolvedFile"
|
||||
}
|
||||
$body = @{
|
||||
dataId = $file.Name
|
||||
group = $Group
|
||||
content = Get-Content -LiteralPath $resolvedFile -Raw -Encoding UTF8
|
||||
type = 'yaml'
|
||||
}
|
||||
if ($Namespace -ne 'public') {
|
||||
$body.tenant = $Namespace
|
||||
}
|
||||
$pairs = foreach ($entry in $body.GetEnumerator()) {
|
||||
[uri]::EscapeDataString([string]$entry.Key) + '=' + [uri]::EscapeDataString([string]$entry.Value)
|
||||
}
|
||||
$result = Invoke-RestMethod -Method Post -Uri $publishUrl `
|
||||
-ContentType 'application/x-www-form-urlencoded; charset=UTF-8' `
|
||||
-Body ($pairs -join '&') -TimeoutSec 10
|
||||
if ([string]$result -ne 'true') {
|
||||
throw "Publish failed: $($file.Name), Nacos response: $result"
|
||||
}
|
||||
$published++
|
||||
$body = @{dataId=$file.Name; group=$Group; tenant=$Namespace; type='yaml'; content=(Get-Content -LiteralPath $file.FullName -Raw -Encoding UTF8); accessToken=$login.accessToken}
|
||||
$pairs = foreach ($entry in $body.GetEnumerator()) { [uri]::EscapeDataString([string]$entry.Key) + '=' + [uri]::EscapeDataString([string]$entry.Value) }
|
||||
$result = Invoke-RestMethod -Method Post -Uri "$base/v1/cs/configs" -ContentType 'application/x-www-form-urlencoded; charset=UTF-8' -Body ($pairs -join '&') -TimeoutSec 10
|
||||
if ([string]$result -ne 'true') { throw "Publish failed: $($file.Name)" }
|
||||
Write-Host "Published $($file.Name) -> $Namespace / $Group"
|
||||
}
|
||||
|
||||
Write-Host "Published $Environment/$Region Nacos configuration: $published items."
|
||||
|
||||
Reference in New Issue
Block a user