整理生产初版初始化与重置流程
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
[switch]$SkipTests,
|
||||
[switch]$AllowDirtyWorkingTree,
|
||||
[switch]$SkipDatabaseInit,
|
||||
[switch]$ResetDatabase,
|
||||
[switch]$ClearOldBackendJars,
|
||||
[string]$DatabaseTargetName = "mysql02",
|
||||
[int]$DatabasePort = 3306,
|
||||
[string]$DatabaseName = "jayu_cloud",
|
||||
@@ -380,7 +382,7 @@ function Invoke-RemoteShell {
|
||||
throw "找不到 PuTTY plink,请安装 PuTTY 工具或改用 SSH key。"
|
||||
}
|
||||
$plinkPath = if ($plink) { $plink.Source } else { "plink" }
|
||||
Invoke-Native $plinkPath @("-batch", "-ssh", "-P", [string]$Server.Port, "-l", $Server.User, "-pw", $Server.Password, $Server.Host, $Command) -Sensitive:$Sensitive -RemoteMutation
|
||||
Invoke-Native $plinkPath @("-batch", "-ssh", "-P", [string]$Server.Port, "-l", $Server.User, "-pw", $Server.Password, $Server.Host, $Command) -Sensitive -RemoteMutation
|
||||
}
|
||||
else {
|
||||
Invoke-Native "ssh" @("-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=accept-new", "-p", [string]$Server.Port, (Get-SshTarget $Server), $Command) -Sensitive:$Sensitive -RemoteMutation
|
||||
@@ -405,7 +407,7 @@ function Copy-FileToRemote {
|
||||
throw "找不到 PuTTY pscp,请安装 PuTTY 工具或改用 SSH key。"
|
||||
}
|
||||
$pscpPath = if ($pscp) { $pscp.Source } else { "pscp" }
|
||||
Invoke-Native $pscpPath @("-batch", "-P", [string]$Server.Port, "-pw", $Server.Password, $LocalPath, "$(Get-SshTarget $Server):$RemotePath") -Sensitive:$Sensitive -RemoteMutation
|
||||
Invoke-Native $pscpPath @("-batch", "-P", [string]$Server.Port, "-pw", $Server.Password, $LocalPath, "$(Get-SshTarget $Server):$RemotePath") -Sensitive -RemoteMutation
|
||||
}
|
||||
else {
|
||||
Invoke-Native "scp" @("-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=accept-new", "-P", [string]$Server.Port, $LocalPath, "$(Get-SshTarget $Server):$RemotePath") -Sensitive:$Sensitive -RemoteMutation
|
||||
@@ -616,6 +618,89 @@ cat /tmp/jayu-cloud-health.out
|
||||
"@
|
||||
}
|
||||
|
||||
function Get-BackendStopCommand {
|
||||
param(
|
||||
[string]$RemoteAppDir,
|
||||
[string]$ServiceName
|
||||
)
|
||||
|
||||
$appDir = ConvertTo-BashSingleQuoted $RemoteAppDir
|
||||
$service = ConvertTo-BashSingleQuoted $ServiceName
|
||||
|
||||
return @"
|
||||
set -e
|
||||
APP_DIR=$appDir
|
||||
SERVICE=$service
|
||||
if command -v systemctl >/dev/null 2>&1 && systemctl list-unit-files | grep -q "^`$SERVICE.service"; then
|
||||
systemctl stop "`$SERVICE" || true
|
||||
fi
|
||||
if [ -f "`$APP_DIR/app.pid" ]; then
|
||||
kill "`$(cat "`$APP_DIR/app.pid")" >/dev/null 2>&1 || true
|
||||
fi
|
||||
pkill -f "java .*`$APP_DIR/.*\.jar" >/dev/null 2>&1 || true
|
||||
sleep 2
|
||||
if pgrep -f "java .*`$APP_DIR/.*\.jar" >/dev/null 2>&1; then
|
||||
echo "Backend process still running under `$APP_DIR"
|
||||
pgrep -af "java .*`$APP_DIR/.*\.jar" || true
|
||||
exit 62
|
||||
fi
|
||||
"@
|
||||
}
|
||||
|
||||
function Get-BackendJarCleanupCommand {
|
||||
param([string]$RemoteAppDir)
|
||||
|
||||
$appDir = ConvertTo-BashSingleQuoted $RemoteAppDir
|
||||
|
||||
return @"
|
||||
set -e
|
||||
APP_DIR=$appDir
|
||||
case "`$APP_DIR" in
|
||||
""|"/"|"/opt"|"/usr"|"/var")
|
||||
echo "Refusing unsafe app dir: `$APP_DIR"
|
||||
exit 63
|
||||
;;
|
||||
esac
|
||||
if [ ! -d "`$APP_DIR" ]; then
|
||||
echo "Backend app dir not found, skip jar cleanup: `$APP_DIR"
|
||||
exit 0
|
||||
fi
|
||||
MANIFEST="`$APP_DIR/jar-cleanup-$releaseTimestamp.txt"
|
||||
find "`$APP_DIR" -maxdepth 3 -type f -name "*.jar" -print | sort > "`$MANIFEST"
|
||||
echo "Jar cleanup manifest: `$MANIFEST"
|
||||
cat "`$MANIFEST" || true
|
||||
find "`$APP_DIR" -maxdepth 3 -type f -name "*.jar" -delete
|
||||
if find "`$APP_DIR" -maxdepth 3 -type f -name "*.jar" | grep -q .; then
|
||||
echo "Jar cleanup failed under `$APP_DIR"
|
||||
find "`$APP_DIR" -maxdepth 3 -type f -name "*.jar" -print
|
||||
exit 64
|
||||
fi
|
||||
"@
|
||||
}
|
||||
|
||||
function Stop-BackendGroup {
|
||||
param(
|
||||
[string]$GroupName,
|
||||
[object[]]$Servers,
|
||||
[string]$RemoteAppDir,
|
||||
[string]$ServiceName
|
||||
)
|
||||
|
||||
Write-Step "停止 $GroupName 服务"
|
||||
Invoke-RemoteShellMany $Servers (Get-BackendStopCommand $RemoteAppDir $ServiceName) $GroupName
|
||||
}
|
||||
|
||||
function Clear-BackendJarGroup {
|
||||
param(
|
||||
[string]$GroupName,
|
||||
[object[]]$Servers,
|
||||
[string]$RemoteAppDir
|
||||
)
|
||||
|
||||
Write-Step "清理 $GroupName 旧 JAR"
|
||||
Invoke-RemoteShellMany $Servers (Get-BackendJarCleanupCommand $RemoteAppDir) $GroupName
|
||||
}
|
||||
|
||||
function Publish-BackendGroup {
|
||||
param(
|
||||
[string]$GroupName,
|
||||
@@ -696,7 +781,8 @@ function Initialize-ProductionDatabase {
|
||||
}
|
||||
|
||||
$remoteSqlPath = "/tmp/jayu-cloud-init-$releaseTimestamp.sql"
|
||||
Write-Step "初始化生产空库"
|
||||
$databaseStepName = if ($ResetDatabase) { "备份并重置生产数据库" } else { "初始化生产空库" }
|
||||
Write-Step $databaseStepName
|
||||
Write-Host "数据库目标:$($DatabaseServer.Name) $($DatabaseServer.Host):$DatabasePort/$DatabaseName"
|
||||
Invoke-RemoteShell $DatabaseServer "rm -f '$remoteSqlPath'"
|
||||
Copy-FileToRemote $SqlArtifact $DatabaseServer $remoteSqlPath -Sensitive
|
||||
@@ -706,24 +792,42 @@ function Initialize-ProductionDatabase {
|
||||
$dbPassword = ConvertTo-BashSingleQuoted $dbPasswordValue
|
||||
$dbPort = [string]$DatabasePort
|
||||
$sqlFile = ConvertTo-BashSingleQuoted $remoteSqlPath
|
||||
$resetDatabaseText = if ($ResetDatabase) { "1" } else { "0" }
|
||||
$command = @'
|
||||
set -e
|
||||
DB_NAME=__DB_NAME__
|
||||
DB_PORT=__DB_PORT__
|
||||
DB_USER=__DB_USER__
|
||||
SQL_FILE=__SQL_FILE__
|
||||
RESET_DATABASE=__RESET_DATABASE__
|
||||
export MYSQL_PWD=__DB_PASSWORD__
|
||||
if ! command -v mysql >/dev/null 2>&1; then
|
||||
echo "mysql client not found on database host"
|
||||
exit 48
|
||||
fi
|
||||
mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 --batch --skip-column-names -e "CREATE DATABASE IF NOT EXISTS \`$DB_NAME\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
||||
TABLE_COUNT=$(mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 --batch --skip-column-names -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${DB_NAME}';")
|
||||
if [ "$TABLE_COUNT" != "0" ]; then
|
||||
echo "Database ${DB_NAME} is not empty: ${TABLE_COUNT} tables. Refusing initialization."
|
||||
rm -f "$SQL_FILE"
|
||||
unset MYSQL_PWD
|
||||
exit 49
|
||||
DB_EXISTS=$(mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 --batch --skip-column-names -e "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name='${DB_NAME}';")
|
||||
if [ "$RESET_DATABASE" = "1" ]; then
|
||||
if ! command -v mysqldump >/dev/null 2>&1; then
|
||||
echo "mysqldump client not found on database host"
|
||||
rm -f "$SQL_FILE"
|
||||
unset MYSQL_PWD
|
||||
exit 50
|
||||
fi
|
||||
if [ "$DB_EXISTS" = "1" ]; then
|
||||
BACKUP_FILE="/tmp/jayu-cloud-${DB_NAME}-backup-__RELEASE_TIMESTAMP__.sql"
|
||||
mysqldump -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 --single-transaction --routines --triggers "$DB_NAME" > "$BACKUP_FILE"
|
||||
echo "Database backup written: $BACKUP_FILE"
|
||||
fi
|
||||
mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 -e "DROP DATABASE IF EXISTS \`$DB_NAME\`; CREATE DATABASE \`$DB_NAME\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
||||
else
|
||||
mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 --batch --skip-column-names -e "CREATE DATABASE IF NOT EXISTS \`$DB_NAME\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
|
||||
TABLE_COUNT=$(mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 --batch --skip-column-names -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='${DB_NAME}';")
|
||||
if [ "$TABLE_COUNT" != "0" ]; then
|
||||
echo "Database ${DB_NAME} is not empty: ${TABLE_COUNT} tables. Refusing initialization."
|
||||
rm -f "$SQL_FILE"
|
||||
unset MYSQL_PWD
|
||||
exit 49
|
||||
fi
|
||||
fi
|
||||
mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 < "$SQL_FILE"
|
||||
ADMIN_COUNT=$(mysql -h 127.0.0.1 -P "$DB_PORT" -u"$DB_USER" --default-character-set=utf8mb4 --batch --skip-column-names "$DB_NAME" -e "SELECT COUNT(*) FROM t_user WHERE account='xiaowang';")
|
||||
@@ -736,6 +840,8 @@ echo "Database initialized. Admin account rows: ${ADMIN_COUNT}"
|
||||
$command = $command.Replace("__DB_USER__", $dbUser)
|
||||
$command = $command.Replace("__DB_PASSWORD__", $dbPassword)
|
||||
$command = $command.Replace("__SQL_FILE__", $sqlFile)
|
||||
$command = $command.Replace("__RESET_DATABASE__", $resetDatabaseText)
|
||||
$command = $command.Replace("__RELEASE_TIMESTAMP__", $releaseTimestamp)
|
||||
Invoke-RemoteShell $DatabaseServer $command -Sensitive
|
||||
}
|
||||
|
||||
@@ -773,6 +879,8 @@ function Write-ReleaseManifest {
|
||||
nacosNamespace = "jayu-cloud"
|
||||
dryRun = [bool]$DryRun
|
||||
deploy = [bool]$Deploy
|
||||
resetDatabase = [bool]$ResetDatabase
|
||||
clearOldBackendJars = [bool]$ClearOldBackendJars
|
||||
}
|
||||
}
|
||||
$manifest | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $manifestPath -Encoding UTF8
|
||||
@@ -822,6 +930,9 @@ if ($webServers.Count -ne 1) {
|
||||
if ((-not $SkipDatabaseInit) -and $databaseServers.Count -ne 1) {
|
||||
throw "数据库初始化目标必须唯一,当前按 $DatabaseTargetName 找到 $($databaseServers.Count) 个。"
|
||||
}
|
||||
if ($ResetDatabase -and $SkipDatabaseInit) {
|
||||
throw "-ResetDatabase 不能和 -SkipDatabaseInit 同时使用。"
|
||||
}
|
||||
|
||||
Assert-ProductionTarget (@($coreServers) + @($gatewayServers) + @($webServers))
|
||||
$databaseServer = if (-not $SkipDatabaseInit) { $databaseServers[0] } else { $null }
|
||||
@@ -845,6 +956,16 @@ if (-not $Deploy) {
|
||||
exit 0
|
||||
}
|
||||
|
||||
if ($ResetDatabase -or $ClearOldBackendJars) {
|
||||
Stop-BackendGroup "Core" $coreServers $CoreRemoteAppDir $CoreServiceName
|
||||
Stop-BackendGroup "Gateway" $gatewayServers $GatewayRemoteAppDir $GatewayServiceName
|
||||
}
|
||||
|
||||
if ($ClearOldBackendJars) {
|
||||
Clear-BackendJarGroup "Core" $coreServers $CoreRemoteAppDir
|
||||
Clear-BackendJarGroup "Gateway" $gatewayServers $GatewayRemoteAppDir
|
||||
}
|
||||
|
||||
if (-not $SkipDatabaseInit) {
|
||||
Initialize-ProductionDatabase $databaseServer $package.InitSql
|
||||
}
|
||||
|
||||
+597
-1081
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user