使用 ConfigSecret.js 存放密码等机密信息

This commit is contained in:
陆柯 2021-07-18 12:58:14 +08:00
parent 6b2a39c673
commit c50d29c072

View File

@ -9,15 +9,15 @@ const deepmerge = require('deepmerge')
const wo = (global.wo = {
envi: {
deploy: {
fromDist: './dist',
fromPath: './dist',
gotoTarget: 'github',
vultr: {
targetType: 'ssh',
host: undefined,
port: 22,
basePath: undefined,
distDir: 'dist',
targetPath: undefined,
targetDir: 'dist',
user: undefined,
password: undefined,
key: `${process.env.HOME}/.ssh/id_rsa`,
@ -43,6 +43,10 @@ try {
wo.envi = deepmerge(wo.envi, require(configFile))
console.info(`${configFile} loaded`)
}
if (fs.existsSync(configFile=path.join(process.cwd(), 'ConfigSecret.js'))) {
wo.envi = deepmerge(wo.envi, require(configFile))
console.info(`${configFile} loaded`)
}
} catch (err) {
console.error('Loading config files failed: ' + err.message)
}
@ -51,15 +55,15 @@ wo.envi.deploy.connection = wo.envi.deploy[wo.envi.deploy.gotoTarget]
// 读取命令行参数
commander
.version('1.0', '-v, --version') // 默认是 -V。如果要 -v就要加 '-v --version'
.option('-f, --fromDist <fromDist>', `local distribution path to copy from. Default to ${wo.envi.deploy.fromDist}`)
.option('-f, --fromPath <fromPath>', `local distribution path to copy from. Default to ${wo.envi.deploy.fromPath}`)
.option('-g, --gotoTarget <gotoTarget>', `connection section in config. Default to ${wo.envi.deploy.gotoTarget}`)
.option('-t, --targetType <targetType>', `target type, git or ssh. Default to ${wo.envi.deploy.connection.targetType}`)
.option('-H, --host <host>', `Host IP or domain name of the target server. Default to ${wo.envi.deploy.connection.host}`)
.option('-P, --port <port>', `Ssh port number of the target server. Default to ${wo.envi.deploy.connection.port}`)
.option('-b, --basePath <basePath>', `Destination path to deploy on the target server. Default to ${wo.envi.deploy.connection.basePath}`)
.option('-d, --distDir <distDir>', `Destination folder to deploy on the target server. Default to ${wo.envi.deploy.connection.distDir}`)
.option('-b, --targetPath <targetPath>', `Destination path to deploy on the target. Default to ${wo.envi.deploy.connection.targetPath}`)
.option('-d, --targetDir <targetDir>', `Destination folder to deploy on the target. Default to ${wo.envi.deploy.connection.targetDir}`)
.option('-r, --repo <repo>', `git repo address. Default to ${wo.envi.deploy.connection.repo}`)
.option('-b, --branch <branch>', `git repo branch. Default to ${wo.envi.deploy.connection.branch}`)
@ -71,35 +75,36 @@ commander
.option('-p, --password <password>', `User password to login the target server. You may have to enclose it in "". Default to "${wo.envi.deploy.connection.password}"`)
.parse(process.argv)
wo.envi.deploy.fromDist = commander.fromDist || wo.envi.deploy.fromDist
wo.envi.deploy.fromPath = commander.fromPath || wo.envi.deploy.fromPath
wo.envi.deploy.connection = wo.envi.deploy[commander.gotoTarget || wo.envi.deploy.gotoTarget] // 使用用户指定的连接
// 可以用命令行参数覆盖掉配置文件
const connection = {
targetType: commander.targetType || wo.envi.deploy.connection.targetType,
// for ssh
host: commander.host || wo.envi.deploy.connection.host,
port: commander.port || wo.envi.deploy.connection.port,
basePath: commander.basePath || wo.envi.deploy.connection.basePath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
distDir: commander.distDir || wo.envi.deploy.connection.distDir, // 新系统将发布在这个文件夹里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。
targetPath: commander.targetPath || wo.envi.deploy.connection.targetPath, // 目标服务器上的目录。似乎该目录必须已经存在于服务器上
targetDir: commander.targetDir || wo.envi.deploy.connection.targetDir, // 新系统将发布在这个文件夹里。建议为dist和npm run build产生的目录一致这样既可以远程自动部署也可以直接登录服务器手动部署。
// for git
repo: commander.repo || wo.envi.deploy.connection.repo,
branch: commander.branch || wo.envi.deploy.connection.branch,
gitname: commander.gitname || wo.envi.deploy.connection.gitname,
gitemail: commander.gitemail || wo.envi.deploy.connection.gitemail,
// common
username: commander.user || wo.envi.deploy.connection.user,
privateKey: fs.existsSync(commander.key || wo.envi.deploy.key) ? (commander.key || wo.envi.deploy.key) : undefined,
password: commander.password || wo.envi.deploy.connection.password,
tryKeyboard: true,
onKeyboardInteractive: (name, instructions, instructionsLang, prompts, finish) => { // 不起作用
onKeyboardInteractive: (name, instructions, lang, prompts, finish) => { // 不起作用
if (prompts.length > 0 && prompts[0].prompt.toLowerCase().includes('password')) {
finish([password])
}
},
}
console.log(` deploy from ${wo.envi.deploy.fromDist} to ${JSON.stringify(connection)}`)
console.log(` deploy from ${wo.envi.deploy.fromPath} to ${JSON.stringify(connection)}`)
if (connection.targetType==='ssh') {
deployToSsh(connection)
@ -133,19 +138,19 @@ function deployToSsh(connection){
}
ssh.connect(connection).then(async () => {
console.log(`[ mv ${connection.distDir} ${connection.distDir}-backup-${new Date().toISOString()} ... ]`)
await ssh.execCommand(`mv ${connection.distDir} ${connection.distDir}-backup-${new Date().toISOString()}`, { cwd: connection.basePath })
console.log(`[ mkdir ${connection.distDir} ... ]`)
await ssh.execCommand(`mkdir ${connection.distDir}`, { cwd: connection.basePath })
const toCreate = necessaryPath(path.join('./', wo.envi.deploy.fromDist))
console.log(`[ mv ${connection.targetDir} ${connection.targetDir}-backup-${new Date().toISOString()} ... ]`)
await ssh.execCommand(`mv ${connection.targetDir} ${connection.targetDir}-backup-${new Date().toISOString()}`, { cwd: connection.targetPath })
console.log(`[ mkdir ${connection.targetDir} ... ]`)
await ssh.execCommand(`mkdir ${connection.targetDir}`, { cwd: connection.targetPath })
const toCreate = necessaryPath(path.join('./', wo.envi.deploy.fromPath))
for (const name of toCreate) {
console.log(`[ mkdir ${connection.distDir}/${name.join('/')} ... ]`)
await ssh.execCommand(`mkdir ${connection.distDir}/${name.join('/')}`, { cwd: connection.basePath })
console.log(`[ mkdir ${connection.targetDir}/${name.join('/')} ... ]`)
await ssh.execCommand(`mkdir ${connection.targetDir}/${name.join('/')}`, { cwd: connection.targetPath })
}
let err
console.log(`[ Upload to ${connection.basePath}/${connection.distDir} ... ]`)
await ssh.putDirectory(path.join('./', wo.envi.deploy.fromDist), `${connection.basePath}/${connection.distDir}`, {
console.log(`[ Upload to ${connection.targetPath}/${connection.targetDir} ... ]`)
await ssh.putDirectory(path.join('./', wo.envi.deploy.fromPath), `${connection.targetPath}/${connection.targetDir}`, {
concurrency: 10,
recursive: true,
validate: itemPath => {
@ -233,9 +238,9 @@ function deployToGit(connection){
}
function exec() {
const baseDir = ''
const deployDir = pathFn.join(baseDir, '.deploy_git')
const fromDir = wo.envi.deploy.fromDist
const targetDir = ''
const deployDir = pathFn.join(targetDir, '.deploy_git')
const fromDir = wo.envi.deploy.fromPath
let extendDirs = connection.extend_dirs
const ignoreHidden = connection.ignore_hidden
const ignorePattern = connection.ignore_pattern
@ -330,7 +335,7 @@ function deployToGit(connection){
const mapFn = function(dir) {
const opts = {}
const extendPath = pathFn.join(baseDir, dir)
const extendPath = pathFn.join(targetDir, dir)
const extendDist = pathFn.join(deployDir, dir)
if (typeof ignoreHidden === 'object') {