137 lines
4.1 KiB
JavaScript
137 lines
4.1 KiB
JavaScript
'use strict';
|
||
|
||
const fs = require('node:fs');
|
||
const path = require('node:path');
|
||
const {
|
||
ROOT,
|
||
ensureCommandAvailable,
|
||
ensureTempDeployDir,
|
||
getNowStamp,
|
||
loadDeployConfig,
|
||
quoteForBash,
|
||
runCommand,
|
||
runScp,
|
||
runSsh,
|
||
} = require('./deploy-lib.cjs');
|
||
|
||
function parseArgs(argv) {
|
||
const options = {
|
||
envName: 'production',
|
||
dryRun: false,
|
||
};
|
||
|
||
for (const item of argv) {
|
||
if (!item) continue;
|
||
if (item === '--dry-run') {
|
||
options.dryRun = true;
|
||
continue;
|
||
}
|
||
if (!item.startsWith('--')) {
|
||
options.envName = item;
|
||
}
|
||
}
|
||
|
||
return options;
|
||
}
|
||
|
||
function listConfigSeedFiles() {
|
||
const output = runCommand('git', [ 'ls-files', '--cached', 'config' ]);
|
||
return output
|
||
.split(/\r?\n/)
|
||
.map(item => item.trim())
|
||
.filter(Boolean)
|
||
.filter(item => item.startsWith('config/'))
|
||
.filter(item => item !== 'config/config.local.js')
|
||
.filter(item => item !== 'config/deploy.config.local.cjs')
|
||
.filter(item => item !== 'config/deploy.config.example.cjs');
|
||
}
|
||
|
||
function buildConfigSeedArchive(stamp, files) {
|
||
const tempDir = ensureTempDeployDir();
|
||
const listPath = path.join(tempDir, `deploy-init-files-${stamp}.txt`);
|
||
const archivePath = path.join(tempDir, `deploy-init-${stamp}.tar.gz`);
|
||
|
||
if (!files || files.length === 0) {
|
||
throw new Error('未找到可用于初始化的 config 文件(git tracked)。');
|
||
}
|
||
|
||
fs.writeFileSync(listPath, files.join('\n') + '\n', 'utf8');
|
||
runCommand('tar', [ '-czf', archivePath, '-T', listPath ], { stdio: 'inherit' });
|
||
return {
|
||
archivePath,
|
||
listPath,
|
||
};
|
||
}
|
||
|
||
function cleanupLocalTemp(paths) {
|
||
for (const filePath of paths) {
|
||
if (!filePath) continue;
|
||
if (fs.existsSync(filePath)) {
|
||
fs.unlinkSync(filePath);
|
||
}
|
||
}
|
||
}
|
||
|
||
function main() {
|
||
const options = parseArgs(process.argv.slice(2));
|
||
|
||
ensureCommandAvailable('git', '请先安装 Git 并确保 git 在 PATH 中。');
|
||
ensureCommandAvailable('tar', '请先安装 tar,Windows 10/11 通常已内置 bsdtar。');
|
||
ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。');
|
||
ensureCommandAvailable('scp', '请先安装 OpenSSH Client 并确保 scp 在 PATH 中。');
|
||
|
||
const config = loadDeployConfig(options.envName);
|
||
const seedFiles = listConfigSeedFiles();
|
||
|
||
if (options.dryRun) {
|
||
console.log(JSON.stringify({
|
||
dryRun: true,
|
||
envName: options.envName,
|
||
deployRoot: config.deployRoot,
|
||
seedFileCount: seedFiles.length,
|
||
seedFiles,
|
||
}, null, 2));
|
||
return;
|
||
}
|
||
|
||
const stamp = getNowStamp();
|
||
const { archivePath, listPath } = buildConfigSeedArchive(stamp, seedFiles);
|
||
const remoteArchive = `${config.incomingDir}/deploy-init-${stamp}.tar.gz`;
|
||
|
||
try {
|
||
const setupScript = [
|
||
'set -euo pipefail',
|
||
`mkdir -p ${quoteForBash(config.deployRoot)} ${quoteForBash(config.controlDir)} ${quoteForBash(config.incomingDir)} ${quoteForBash(config.backupDir)} ${quoteForBash(config.stateDir)} ${quoteForBash(path.posix.join(config.deployRoot, 'logs'))} ${quoteForBash(path.posix.join(config.deployRoot, 'run'))} ${quoteForBash(path.posix.join(config.deployRoot, 'temp'))}`,
|
||
].join('\n');
|
||
runSsh(config, setupScript);
|
||
runScp(config, archivePath, remoteArchive);
|
||
|
||
const remoteScript = [
|
||
'set -euo pipefail',
|
||
`DEPLOY_ROOT=${quoteForBash(config.deployRoot)}`,
|
||
`REMOTE_ARCHIVE=${quoteForBash(remoteArchive)}`,
|
||
'CONFIG_DIR="$DEPLOY_ROOT/config"',
|
||
'if [ -d "$CONFIG_DIR" ] && [ "$(find "$CONFIG_DIR" -mindepth 1 -maxdepth 1 | wc -l | tr -d \" \" )" -gt 0 ]; then',
|
||
' echo DEPLOY_INIT_CONFIG_SKIPPED_EXISTS',
|
||
'else',
|
||
' mkdir -p "$CONFIG_DIR"',
|
||
' tar -xzf "$REMOTE_ARCHIVE" -C "$DEPLOY_ROOT"',
|
||
' echo DEPLOY_INIT_CONFIG_CREATED',
|
||
'fi',
|
||
'rm -f "$REMOTE_ARCHIVE"',
|
||
].join('\n');
|
||
|
||
const output = runSsh(config, remoteScript, { stdio: 'pipe' });
|
||
for (const line of output.split(/\r?\n/)) {
|
||
if (line.trim()) {
|
||
console.log(line.trim());
|
||
}
|
||
}
|
||
} finally {
|
||
cleanupLocalTemp([ archivePath, listPath ]);
|
||
}
|
||
|
||
console.log(`首次发布初始化完成: ${options.envName} -> ${config.deployRoot}`);
|
||
}
|
||
|
||
main(); |