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