Files
inquiry_robot/deploy/_remote_db_intranet_align.ps1
T

98 lines
4.5 KiB
PowerShell

# Align DB hosts to intranet IP; diagnose MySQL create state. No secret stdout.
$ErrorActionPreference = "Continue"
$mysql = "C:\yutongda\tools\mysql-8.0.39-winx64\bin\mysql.exe"
$psql = "C:\yutongda\tools\pgsql\bin\psql.exe"
$dbCred = Get-Content "C:\yutongda\secure\application-database-credentials.json" -Raw | ConvertFrom-Json
$sec = Get-Content "E:\wwwroot\ops\inquiry_robot_db_secrets.json" -Raw | ConvertFrom-Json
Write-Output "=== this host intranet addresses ==="
Get-NetIPAddress -AddressFamily IPv4 -EA SilentlyContinue |
Where-Object { $_.IPAddress -notlike '127.*' } |
ForEach-Object { "$($_.InterfaceAlias) $($_.IPAddress)" }
Write-Output "=== credential hosts (from secure json) ==="
Write-Output ("mysql_host=" + $dbCred.mysql_host + " postgres_host=" + $dbCred.postgres_host)
Write-Output "=== .env host lines now ==="
Select-String -Path "E:\wwwroot\.env" -Pattern '^(MYSQL_HOST|PG_HOST|YTD_MYSQL_URL)=' |
ForEach-Object {
$l = $_.Line
if ($l -match 'PASSWORD') { ($l -split '=',2)[0] + '=***' } else { $l }
}
function Test-Mysql([string]$host, [string]$user, [string]$pass, [string]$db) {
$args = @("-h$host","-P3306","-u$user","-p$pass","--connect-timeout=8","--batch","-N")
if ($db) { $args += $db }
$args += @("-e","SELECT 1")
$out = & $mysql @args 2>&1
$text = ($out | ForEach-Object {"$_"}) -join "`n"
if ($text -match "ERROR\s+\d+") {
$err = ($text -split "`n" | Where-Object { $_ -match "ERROR" } | Select-Object -First 1)
return "FAIL $err"
}
$lines = $out | ForEach-Object {"$_".Trim()} | Where-Object { $_ -ne "" -and $_ -notmatch "Warning" }
if ($lines -contains "1") { return "OK" }
return "FAIL no-result"
}
function Test-RootCreate([string]$host, [string]$pass) {
$out = & $mysql -h$host -P3306 -uroot "-p$pass" --connect-timeout=8 --batch -N -e "SELECT 1" 2>&1
$text = ($out | ForEach-Object {"$_"}) -join "`n"
if ($text -match "ERROR") { return "root_fail" }
$lines = $out | ForEach-Object {"$_".Trim()} | Where-Object { $_ -ne "" -and $_ -notmatch "Warning" }
if ($lines -notcontains "1") { return "root_fail" }
$dbs = & $mysql -h$host -P3306 -uroot "-p$pass" --batch -N -e "SHOW DATABASES LIKE 'inquiry_robot'" 2>&1
$has = (($dbs | ForEach-Object {"$_"}) -join " ") -match "inquiry_robot"
return ("root_ok has_db=" + $has)
}
$hosts = @("127.0.0.1", "10.206.0.15", [string]$dbCred.mysql_host)
$hosts = $hosts | Select-Object -Unique
Write-Output "=== inquiry_robot app user smoke ==="
foreach ($h in $hosts) {
Write-Output ("host=$h -> " + (Test-Mysql $h "inquiry_robot" ([string]$sec.mysql_password) "inquiry_robot"))
}
Write-Output "=== root smoke if local secret exists ==="
$rootPass = $null
if (Test-Path "E:\wwwroot\ops\local_mysql_root_secret.json") {
$rootPass = [string](Get-Content "E:\wwwroot\ops\local_mysql_root_secret.json" -Raw | ConvertFrom-Json).root_password
Write-Output "local_mysql_root_secret=present"
} elseif (Test-Path "E:\wwwroot\ops\.mysql_root_stash.tmp") {
$rootPass = (Get-Content "E:\wwwroot\ops\.mysql_root_stash.tmp" -Raw).Trim()
Write-Output "stash=present"
} else {
Write-Output "no_root_secret"
}
if ($rootPass) {
foreach ($h in $hosts) {
Write-Output ("root@$h -> " + (Test-RootCreate $h $rootPass))
}
}
Write-Output "=== ytd_jeecg on data host show dbs like inquiry ==="
$h = [string]$dbCred.mysql_host
$u = [string]$dbCred.mysql_username
$p = [string]$dbCred.mysql_password
& $mysql -h$h -P3306 -u$u "-p$p" --batch -e "SHOW DATABASES LIKE 'inquiry%'; SHOW DATABASES LIKE 'jeecg%';" 2>&1 |
Where-Object { $_ -notmatch "Warning" } | ForEach-Object { "$_" }
Write-Output "=== PG inquiry_robot_runtime ==="
$env:PGPASSWORD = [string]$sec.pg_password
foreach ($h in @("127.0.0.1","10.206.0.15",[string]$dbCred.postgres_host) | Select-Object -Unique) {
$r = & $psql -h $h -p 5432 -U inquiry_robot_runtime -d inquiry_robot_runtime -tAc "SELECT 'pg_ok'" 2>&1
Write-Output ("pg@$h -> " + (($r | ForEach-Object {"$_"}) -join " "))
}
# also postgres superuser local
$env:PGPASSWORD = (Get-Content "C:\yutongda\secure\pgpass.txt" -Raw).Trim()
foreach ($h in @("127.0.0.1","10.206.0.15",[string]$dbCred.postgres_host) | Select-Object -Unique) {
$r = & $psql -h $h -p 5432 -U postgres -d postgres -tAc "SELECT datname FROM pg_database WHERE datname='inquiry_robot_runtime'" 2>&1
Write-Output ("pg_super@$h inquiry_robot_runtime=" + (($r | ForEach-Object {"$_"}) -join " ").Trim())
}
Remove-Item Env:PGPASSWORD -EA SilentlyContinue
Write-Output "=== YTD-MySQL service ==="
Get-Service YTD-MySQL | Format-Table Name,Status -AutoSize | Out-String | Write-Output
Write-Output "DONE"