更新服务

This commit is contained in:
aixianling
2022-04-01 16:05:56 +08:00
parent 94ce411071
commit 948cc94338
19 changed files with 137 additions and 76 deletions

1
.foreverignore Normal file
View File

@@ -0,0 +1 @@
/zips/

View File

@@ -1,6 +1,6 @@
const express = require('express') const express = require('express')
const db = require('./utils/dbUitls') const db = require('./src/utils/dbUitls')
const rest = require('./rest') const rest = require('./src/rest')
const app = express() const app = express()
const port = 12525 const port = 12525

View File

@@ -4,7 +4,7 @@
"description": "node服务端", "description": "node服务端",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"dev": "forever -w index.js", "dev": "forever -w --watchDirectory src index.js ",
"pro": "forever start index.js" "pro": "forever start index.js"
}, },
"repository": { "repository": {
@@ -20,6 +20,7 @@
}, },
"author": "kubbo", "author": "kubbo",
"dependencies": { "dependencies": {
"archiver": "^5.3.0",
"express": "^4.17.3", "express": "^4.17.3",
"fs-extra": "^10.0.1", "fs-extra": "^10.0.1",
"helmet": "^5.0.2", "helmet": "^5.0.2",

View File

@@ -1,30 +0,0 @@
const dbUtils = require("../../utils/dbUitls");
const fse = require("fs-extra");
module.exports = {
action: "/node/autodeploy/download",
method: "post",
execute: (request, response) => {
let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'`
dbUtils.query(sql).then(res => {
let info = res?.[0]
if (info?.id) {
response.send({code: 0})
fse.emptyDir('../zips/dist', err => {
if (!err) {
execute(`cd ../zips&&git clone ${info.git} ./dist`)
.then(() => execute(`cd ../zips/dist&&git checkout ${info.branch}`))
.then(() => execute(`cd ../zips/dist&&npm i&&npm run build`))
.catch(err => {
console.log(err)
})
} else {
console.log(err)
}
})
} else response.send({code: 1, err: "无法找到git信息"})
}).catch(err => {
console.log(err)
response.send({code: 1, err: err.sqlMessage})
})
}
}

View File

@@ -1,39 +0,0 @@
const dbUtils = require("../../utils/dbUitls");
const {exec} = require("child_process");
const fse = require('fs-extra')
const execute = cmd => new Promise((resolve, reject) => {
exec(cmd, (err, stdout) => {
if (!err) {
console.log(stdout)
resolve()
} else reject(err)
})
})
module.exports = {
action: "/node/autodeploy/getZip",
method: "post",
execute: (request, response) => {
let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'`
dbUtils.query(sql).then(res => {
let info = res?.[0]
if (info?.id) {
response.send({code: 0})
fse.emptyDir('../zips/' + info.id, err => {
if (!err) {
execute(`cd ../zips&&git clone ${info.git} ./${info.id}`)
.then(() => execute(`cd ../zips/${info.id}&&git checkout ${info.branch}`))
.then(() => execute(`cd ../zips/${info.id}&&npm i&&npm run build`))
.catch(err => {
console.log(err)
})
} else {
console.log(err)
}
})
} else response.send({code: 1, err: "无法找到git信息"})
}).catch(err => {
console.log(err)
response.send({code: 1, err: err.sqlMessage})
})
}
}

View File

@@ -8,7 +8,10 @@ module.exports = {
dbUtils.query(sql).then(res => { dbUtils.query(sql).then(res => {
let info = res?.[0] let info = res?.[0]
if (info?.id) { if (info?.id) {
response.send({code: 0}) fse.pathExists(`zips/${info.id}/dist`, (err, exists) => {
console.log(`zips/${info.id}/dist=========>${exists}`)
response.send({code: 0, data: exists})
})
} else response.send({code: 1, err: "无法找到git信息"}) } else response.send({code: 1, err: "无法找到git信息"})
}).catch(err => { }).catch(err => {
console.log(err) console.log(err)

View File

@@ -0,0 +1,40 @@
const dbUtils = require("../../utils/dbUitls");
const archiver = require("archiver")
const fse = require("fs-extra");
const fs = require("fs");
module.exports = {
action: "/node/autodeploy/download",
method: "post",
execute: (request, response) => {
let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'`
dbUtils.query(sql).then(res => {
let info = res?.[0]
if (info?.id) {
let path = `zips/${info.id}/dist`, zipPath = `./zips/${info.id}.zip`
fse.removeSync(zipPath)
fse.pathExists(path, (err, exists) => {
console.log(`${path}=========>${exists}`)
if (exists) {
let output = fs.createWriteStream(zipPath),
arc = archiver('zip')
arc.on('error', err => {
response.send({code: 1, err})
})
arc.on('progress', (e) => {
console.log('%s压缩进度...%s/%s', info.id, e.entries.processed, e.entries.total)
})
arc.pipe(output)
arc.directory(path, false)
arc.finalize().then(() => {
console.log('压缩完成!')
response.download(zipPath, `${info.id}.zip`)
})
} else response.send({code: 1, err: "没有打包文件!"})
})
} else response.send({code: 1, err: "无法找到git信息"})
}).catch(err => {
console.log(err)
response.send({code: 1, err: err.sqlMessage})
})
}
}

View File

@@ -0,0 +1,25 @@
const db = require("../../utils/dbUitls");
const zip = require("../../tools/zipProject")
module.exports = {
action: "/node/autodeploy/getZip",
method: "post",
execute: (request, response) => {
let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'`
db.query(sql).then(res => {
let info = res?.[0]
if (info?.id) {
setTimeout(() => {
response.send({code: 0})
}, 2000)
zip(info).then(() => {
db.query(`update node_autodeploy set download='${new Date()}' where id='${info.id}'`)
}).catch(err => {
console.log(err)
})
} else response.send({code: 1, err: "无法找到git信息"})
}).catch(err => {
console.log(err)
response.send({code: 1, err: err.sqlMessage})
})
}
}

View File

@@ -0,0 +1,29 @@
const dbUtils = require("../../utils/dbUitls");
const zip = require("../../tools/zipProject");
const db = require("../../utils/dbUitls");
const exec = require("../../tools/exec")
module.exports = {
action: "/node/autodeploy/updateSystem",
method: "post",
execute: (request, response) => {
let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'`
dbUtils.query(sql).then(res => {
let info = res?.[0]
if (info?.id) {
setTimeout(() => {
response.send({code: 0})
}, 2000)
zip(info).then(() => {
db.query(`update node_autodeploy set download='${new Date()}' where id='${info.id}'`)
exec(`cp -r zips/${info.id}/dist ${info.target}`).then(() => {
console.log("部署完毕!")
})
})
} else response.send({code: 1, err: "无法找到git信息"})
}).catch(err => {
console.log(err)
response.send({code: 1, err: err.sqlMessage})
})
}
}

View File

@@ -1,10 +1,10 @@
const {findFile} = require("../utils/fsUtils"); const {findFile} = require("../utils/fsUtils");
module.exports = { module.exports = {
init: ins => { init: ins => {
return findFile('./rest', file => { return findFile('./src/rest', file => {
if (!/index\.js/.test(file)) { if (!/index\.js/.test(file)) {
let rest = require(file.replace(/rest/, '.')) let rest = require(file.replace(/src[\\\/]rest/, '.'))
console.log(`初始化接口...${rest.action}`) console.log(`初始化接口:${rest.action}`)
if (rest.method == "post") { if (rest.method == "post") {
ins.post(rest.action, (req, res) => rest.execute(req, res)) ins.post(rest.action, (req, res) => rest.execute(req, res))
} }

10
src/tools/exec.js Normal file
View File

@@ -0,0 +1,10 @@
const {exec} = require("child_process");
const execute = cmd => new Promise((resolve, reject) => {
exec(cmd, (err, stdout) => {
if (!err) {
console.log(stdout)
resolve()
} else reject(err)
})
})
module.exports = execute

21
src/tools/zipProject.js Normal file
View File

@@ -0,0 +1,21 @@
const fse = require("fs-extra");
const execute = require("./exec")
module.exports = info => {
return new Promise((resolve, reject) => {
fse.emptyDir(`zips/${info.id}`, err => {
if (!err) {
execute(`cd zips&&git clone ${info.git} ./${info.id}`)
.then(() => execute(`cd zips/${info.id}&&git checkout ${info.branch}`))
.then(() => execute(`cd zips/${info.id}&&npm i&&npm run build`))
.then(() => resolve())
.catch(errs => {
console.log(errs)
reject(errs)
})
} else {
reject(err)
console.log(err)
}
})
})
}