更新服务
This commit is contained in:
8
src/config/db.js
Normal file
8
src/config/db.js
Normal file
@@ -0,0 +1,8 @@
|
||||
module.exports = {
|
||||
host: "192.168.1.87",
|
||||
user: "root",
|
||||
port: 3306,
|
||||
password: "Cwy@2019",
|
||||
database: "dvcp_oms_dev",
|
||||
multipleStatements: true
|
||||
}
|
||||
27
src/rest/autodeploy/add.js
Normal file
27
src/rest/autodeploy/add.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const dbUtils = require("../../utils/dbUitls");
|
||||
const {v4: uuid} = require('uuid');
|
||||
module.exports = {
|
||||
action: "/node/autodeploy/addOrUpdate",
|
||||
method: "post",
|
||||
execute: (request, response) => {
|
||||
let form = request.body, sql
|
||||
if (form.id) {//编辑
|
||||
let arr = Object.keys(form).filter(e => form[e]).map(e => `${e}='${form[e]}'`)
|
||||
sql = `update node_autodeploy set ${arr.join(",")} where id='${form.id}'`
|
||||
} else {//新增
|
||||
let cols = [], arr = []
|
||||
Object.keys(form).map(e => {
|
||||
if (form[e]) {
|
||||
cols.push(e)
|
||||
arr.push(`'${form[e]}'`)
|
||||
}
|
||||
})
|
||||
sql = `insert into node_autodeploy (id,${cols.join(",")}) values('${uuid()}',${arr.join(",")})`
|
||||
}
|
||||
dbUtils.query(sql).then(() => {
|
||||
response.send({code: 0})
|
||||
}).catch(err => {
|
||||
response.send({code: 1, err: err.sqlMessage})
|
||||
})
|
||||
}
|
||||
}
|
||||
21
src/rest/autodeploy/confirmZip.js
Normal file
21
src/rest/autodeploy/confirmZip.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const dbUtils = require("../../utils/dbUitls");
|
||||
const fse = require("fs-extra");
|
||||
module.exports = {
|
||||
action: "/node/autodeploy/confirmZip",
|
||||
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) {
|
||||
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信息"})
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
response.send({code: 1, err: err.sqlMessage})
|
||||
})
|
||||
}
|
||||
}
|
||||
14
src/rest/autodeploy/delete.js
Normal file
14
src/rest/autodeploy/delete.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const dbUtils = require("../../utils/dbUitls");
|
||||
module.exports = {
|
||||
action: "/node/autodeploy/delete",
|
||||
method: "post",
|
||||
execute: (request, response) => {
|
||||
let {query: {ids}} = request
|
||||
ids = ids?.split(",")?.map(e => `'${e}'`)?.toString()
|
||||
dbUtils.query(`delete from node_autodeploy where id in (${ids})`).then(() => {
|
||||
response.send({code: 0})
|
||||
}).catch(err => {
|
||||
response.send({code: 1, err: err.sqlMessage})
|
||||
})
|
||||
}
|
||||
}
|
||||
40
src/rest/autodeploy/download.js
Normal file
40
src/rest/autodeploy/download.js
Normal 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})
|
||||
})
|
||||
}
|
||||
}
|
||||
25
src/rest/autodeploy/getZip.js
Normal file
25
src/rest/autodeploy/getZip.js
Normal 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})
|
||||
})
|
||||
}
|
||||
}
|
||||
28
src/rest/autodeploy/list.js
Normal file
28
src/rest/autodeploy/list.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const dbUtils = require("../../utils/dbUitls");
|
||||
module.exports = {
|
||||
action: "/node/autodeploy/list",
|
||||
method: "post",
|
||||
execute: (request, response) => {
|
||||
let total = 0, records = [], {size, current, name} = request.query
|
||||
Promise.all([
|
||||
dbUtils.query(`select 1 from node_autodeploy`).then(res => {
|
||||
return total = res.length
|
||||
}),
|
||||
new Promise(resolve => {
|
||||
let sql = `select * from node_autodeploy where name like '%${name}%' limit ${(current-1)*size},${size}`
|
||||
dbUtils.query(sql).then(res => {
|
||||
records = res
|
||||
resolve()
|
||||
}).catch(err => {
|
||||
response.send({code: 1, err: err.sqlMessage})
|
||||
})
|
||||
})
|
||||
|
||||
]).then(() => {
|
||||
response.send({
|
||||
code: 0,
|
||||
data: {records, total}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
29
src/rest/autodeploy/updateSystem.js
Normal file
29
src/rest/autodeploy/updateSystem.js
Normal 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})
|
||||
})
|
||||
}
|
||||
}
|
||||
16
src/rest/index.js
Normal file
16
src/rest/index.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const {findFile} = require("../utils/fsUtils");
|
||||
module.exports = {
|
||||
init: ins => {
|
||||
return findFile('./src/rest', file => {
|
||||
if (!/index\.js/.test(file)) {
|
||||
let rest = require(file.replace(/src[\\\/]rest/, '.'))
|
||||
console.log(`初始化接口:${rest.action}`)
|
||||
if (rest.method == "post") {
|
||||
ins.post(rest.action, (req, res) => rest.execute(req, res))
|
||||
}
|
||||
}
|
||||
}).then(() => {
|
||||
console.log("接口初始化完毕")
|
||||
})
|
||||
}
|
||||
}
|
||||
13
src/rest/sysuser/sysuser.js
Normal file
13
src/rest/sysuser/sysuser.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const dbUtils = require("../../utils/dbUitls");
|
||||
module.exports = {
|
||||
action: "/sys/user",
|
||||
method: "post",
|
||||
execute: (request, response) => {
|
||||
dbUtils.query(`select * from sys_user`).then(res => {
|
||||
response.send({
|
||||
code: 0,
|
||||
data: res
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
10
src/tools/exec.js
Normal file
10
src/tools/exec.js
Normal 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
21
src/tools/zipProject.js
Normal 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)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
25
src/utils/dbUitls.js
Normal file
25
src/utils/dbUitls.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const mysql = require("mysql");
|
||||
const dbConfig = require("../config/db");
|
||||
module.exports = {
|
||||
pool: null,
|
||||
init: () => {
|
||||
this.pool = mysql.createPool(dbConfig)
|
||||
},
|
||||
query: sql => new Promise((resolve, reject) => {
|
||||
this.pool?.getConnection((err, conn) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
} else {
|
||||
conn.query(sql, (err, result) => {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
reject(err)
|
||||
} else {
|
||||
conn.release()
|
||||
resolve(result)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}),
|
||||
}
|
||||
37
src/utils/fsUtils.js
Normal file
37
src/utils/fsUtils.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
|
||||
const promisify = fn => {
|
||||
return function () {
|
||||
let args = arguments;
|
||||
return new Promise(function (resolve, reject) {
|
||||
[].push.call(args, function (err, result) {
|
||||
if (err) {
|
||||
console.log(err)
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
fn.apply(null, args);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const readdir = promisify(fs.readdir)
|
||||
const stat = promisify(fs.stat)
|
||||
const findFile = (dir = '.', cb) => {
|
||||
return readdir(dir).then(apps => {
|
||||
return Promise.all(apps.map(e => {
|
||||
let cPath = path.join(dir, e)
|
||||
return stat(cPath).then(state => {
|
||||
if (state.isDirectory()) {
|
||||
return findFile(cPath, cb)
|
||||
} else if (state.isFile()) {
|
||||
cb && cb(cPath)
|
||||
}
|
||||
})
|
||||
}))
|
||||
})
|
||||
}
|
||||
module.exports = {readdir, stat, findFile}
|
||||
Reference in New Issue
Block a user