132 lines
3.8 KiB
JavaScript
132 lines
3.8 KiB
JavaScript
/**
|
|
* 上传 deploy-pack.tar.gz 到服务器并解压
|
|
* 用法(密钥): node scripts/upload-deploy.mjs
|
|
* 用法(密码): $env:DEPLOY_SSH_PASS='你的root密码'; node scripts/upload-deploy.mjs
|
|
*/
|
|
import { readFileSync, existsSync } from 'node:fs'
|
|
import { homedir } from 'node:os'
|
|
import { join, dirname } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
import { Client } from 'ssh2'
|
|
|
|
const __dir = dirname(fileURLToPath(import.meta.url))
|
|
const webRoot = join(__dir, '..')
|
|
const packPath = join(webRoot, 'deploy-pack.tar.gz')
|
|
const defaultKey = join(homedir(), '.ssh', 'id_ed25519_umed')
|
|
|
|
const config = {
|
|
host: process.env.DEPLOY_SSH_HOST || '47.86.202.232',
|
|
port: Number(process.env.DEPLOY_SSH_PORT || 22),
|
|
username: process.env.DEPLOY_SSH_USER || 'root',
|
|
password: process.env.DEPLOY_SSH_PASS || '',
|
|
privateKeyPath: process.env.DEPLOY_SSH_KEY || defaultKey,
|
|
remoteDir:
|
|
process.env.DEPLOY_REMOTE_DIR ||
|
|
'/opt/1panel/apps/openresty/openresty/www/sites/umedglobal.cn/index',
|
|
}
|
|
|
|
function getConnectOptions() {
|
|
const opts = {
|
|
host: config.host,
|
|
port: config.port,
|
|
username: config.username,
|
|
}
|
|
if (config.password) {
|
|
opts.password = config.password
|
|
return opts
|
|
}
|
|
if (existsSync(config.privateKeyPath)) {
|
|
opts.privateKey = readFileSync(config.privateKeyPath)
|
|
return opts
|
|
}
|
|
return null
|
|
}
|
|
|
|
function execCommand(conn, command) {
|
|
return new Promise((resolve, reject) => {
|
|
conn.exec(command, (err, stream) => {
|
|
if (err) return reject(err)
|
|
let stdout = ''
|
|
let stderr = ''
|
|
stream
|
|
.on('close', (code) => {
|
|
if (code === 0) resolve(stdout)
|
|
else reject(new Error(stderr || stdout || `命令失败 (exit ${code}): ${command}`))
|
|
})
|
|
.on('data', (d) => {
|
|
stdout += d.toString()
|
|
process.stdout.write(d)
|
|
})
|
|
stream.stderr.on('data', (d) => {
|
|
stderr += d.toString()
|
|
process.stderr.write(d)
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
function uploadFile(conn, localPath, remotePath) {
|
|
return new Promise((resolve, reject) => {
|
|
conn.sftp((err, sftp) => {
|
|
if (err) return reject(err)
|
|
const data = readFileSync(localPath)
|
|
sftp.writeFile(remotePath, data, (writeErr) => {
|
|
sftp.end()
|
|
if (writeErr) reject(writeErr)
|
|
else resolve()
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
async function main() {
|
|
if (!existsSync(packPath)) {
|
|
console.error('缺少 deploy-pack.tar.gz,请先打包: tar -czf deploy-pack.tar.gz .output .env')
|
|
process.exit(1)
|
|
}
|
|
|
|
const connectOpts = getConnectOptions()
|
|
if (!connectOpts) {
|
|
console.error('无法连接:请设置 DEPLOY_SSH_PASS,或在服务器添加部署密钥后重试')
|
|
console.error('密钥路径:', config.privateKeyPath)
|
|
process.exit(1)
|
|
}
|
|
|
|
const conn = new Client()
|
|
await new Promise((resolve, reject) => {
|
|
conn.on('ready', resolve).on('error', reject).connect(connectOpts)
|
|
})
|
|
|
|
try {
|
|
console.log('SSH 已连接')
|
|
console.log(`创建远程目录 ${config.remoteDir} ...`)
|
|
await execCommand(conn, `mkdir -p ${config.remoteDir}`)
|
|
|
|
const remoteTar = `${config.remoteDir}/deploy-pack.tar.gz`
|
|
const sizeMb = (readFileSync(packPath).length / 1024 / 1024).toFixed(1)
|
|
console.log(`上传 ${sizeMb} MB -> ${remoteTar}`)
|
|
await uploadFile(conn, packPath, remoteTar)
|
|
|
|
console.log('上传完成,正在解压...')
|
|
await execCommand(
|
|
conn,
|
|
[
|
|
`cd ${config.remoteDir}`,
|
|
'tar -xzf deploy-pack.tar.gz',
|
|
'rm -f deploy-pack.tar.gz',
|
|
'ls -la .output/server/index.mjs .env',
|
|
'echo DEPLOY_EXTRACT_OK',
|
|
].join(' && ')
|
|
)
|
|
|
|
console.log('部署包已上传并解压到', config.remoteDir)
|
|
} catch (error) {
|
|
console.error('部署失败:', error.message)
|
|
process.exit(1)
|
|
} finally {
|
|
conn.end()
|
|
}
|
|
}
|
|
|
|
main()
|