webio-node/route/api/server.js

362 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2017-04-13 05:23:08 +00:00
const express = require('express');
const router = express.Router();
const rt = require('../ResTool');
const config = require('../../config.json');
const fs = require('fs');
const mysql = require('../../libs/mysql_pool');
const tool = require('../../includes/apiTool');
const exec = require('child_process').exec;
const so = require('../../includes/storeObject');
const crypt = require('../../libs/crypto');
router
.get('/', (req, res, n) => {
res.send({ name: 'WebIO Server IPMI/SNMP API' });
})
.post('*', async(req, res, n) => {
try {
res.db = await mysql.getConn();
} catch (e) {
console.log(`Get DB Connection ERROR ${e}`);
return n('ERR8100');
}
n();
})
2017-05-22 09:47:16 +00:00
.post('/getdevicelist', (req, res, n) => {
2017-04-13 05:23:08 +00:00
if (!config.permission.server) return n('ERR9000');
let query = "select * from ??.??";
2017-05-22 09:47:16 +00:00
let param = [config.db.db11, 'dev'];
2017-04-13 05:23:08 +00:00
res.db.query(query, param, (err, row) => {
if (err) return rt.err(res, err, n, 'ERR8000');
res.api_res = {
record: tool.checkArray(row)
}
n();
})
})
2017-05-22 09:47:16 +00:00
.post('/getdevice', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
let query = "select * from ??.?? where `devuid` = ?";
let param = [config.db.db11, 'dev', arr.data.id];
try {
let dev = await tool.promiseQuery(res, query, param);
if (dev.data.length == 0) return n('ERR8000');
let ipmi_param = [config.db.db11, 'ipmi', arr.data.id];
let ipmi = await tool.promiseQuery(res, query, ipmi_param);
let snmp_param = [config.db.db11, 'snmp', arr.data.id];
let snmp = await tool.promiseQuery(res, query, snmp_param);
res.api_res = {
record: tool.checkArray(dev.data),
rt: {
ipmi: tool.checkArray(ipmi.data),
snmp: tool.checkArray(snmp.data)
}
}
} catch (e) {
return rt.err(res, e, n, 'ERR8000');
}
n();
})
.post('/adddevice', async(req, res, n) => {
2017-04-13 05:23:08 +00:00
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.name) return n('ERR0026');
2017-05-22 09:47:16 +00:00
let u = '';
let obj = so.get(req.headers['x-auth-token'] || '');
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
try {
let query = "insert into ??.?? (`name`, `ctime`, `cuser`, `mtime`, `muser`) values \
(?, unix_timestamp(), ?, unix_timestamp(), ?)";
let param = [config.db.db11, 'dev', arr.data.name, u, u];
await tool.promiseQuery(res, query, param);
} catch (e) {
return rt.err(res, e, n, 'ERR8001');
2017-04-13 05:23:08 +00:00
}
2017-04-14 10:09:20 +00:00
2017-05-22 09:47:16 +00:00
res.api_res = {
record: []
};
n();
})
.post('/editdevice', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.name) return n('ERR0026');
let u = '';
let obj = so.get(req.headers['x-auth-token'] || '');
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
try {
let query = "update ??.?? set \
`name` = ?, \
`muser` = ?, \
`mtime` = unix_timestamp() \
where \
`devuid` = ?";
let param = [config.db.db11, 'dev', arr.data.name, u, arr.data.id];
await tool.promiseQuery(res, query, param);
} catch (e) {
return rt.err(res, e, n, 'ERR8002');
}
res.api_res = {
record: []
};
n();
})
.post('/deldevice', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
try {
2017-05-25 05:37:16 +00:00
let query = "delete from ??.?? where `devuid` = ?";
let param = [config.db.db11, 'dev', arr.data.id];
await tool.promiseQuery(res, query, param);
} catch (err) {
return rt.err(res, err, n, 'ERR8003');
}
2017-05-22 09:47:16 +00:00
2017-05-25 05:37:16 +00:00
res.api_res = {
record: []
}
return n();
})
.post('/addipmi', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.devuid) return n('ERR0028');
if (!arr.data.account) return n('ERR0016');
if (!arr.data.password) return n('ERR0017');
if (!arr.data.ip) return n('ERR0010');
try {
let query = "select count(*) as c from ??.?? where `ip` = ?";
let param = [config.db.db11, 'ipmi', arr.data.ip];
let c = await tool.promiseQuery(res, query, param);
if (c.data.length > 0 && c.data.c > 0) return n('ERR0027');
} catch (err) {
return rt.err(res, err, n, 'ERR8000');
}
let u = '';
let obj = so.get(req.headers['x-auth-token'] || '');
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
try {
let query = "insert into ??.?? (`devuid`, `ip`, `account`, `password`, `cuser`, `ctime`, `muser`, `mtime`) values \
(?, ?, ?, ?, ?, unix_timestamp(), ?, unix_timestamp())";
let param = [config.db.db11, 'ipmi', arr.data.devuid, arr.data.ip, arr.data.account, arr.data.password, u, u];
await tool.promiseQuery(res, query, param);
} catch (err) {
return rt.err(res, err, n, 'ERR8001');
}
res.api_res = {
record: []
};
return n();
})
.post('/editipmi', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
if (!arr.data.account) return n('ERR0016');
if (!arr.data.password) return n('ERR0017');
if (!arr.data.ip) return n('ERR0010');
try {
let query = "select count(*) as c from ??.?? where `ipmuid` != ? and `ip` = ?";
let param = [config.db.db11, 'ipmi', arr.data.id, arr.data.ip];
let c = await tool.promiseQuery(res, query, param);
if (c.data.length > 0 && c.data.c > 0) return n('ERR0027');
} catch (err) {
return rt.err(res, err, n, 'ERR8000');
}
let u = '';
let obj = so.get(req.headers['x-auth-token'] || '');
if (obj != null && 'user' in obj && 'account' in obj.user) u = obj.user.account;
try {
let query = "update ??.?? set \
`account` = ?, \
`password` = ?, \
`ip` = ?, \
`muser` = ?, \
`mtime` = unix_timestamp() \
where \
`ipmuid` = ?";
let param = [config.db.db11, 'ipmi', arr.data.account, arr.data.password, arr.data.ip, u, arr.data.id];
await tool.promiseQuery(res, query, param);
} catch (err) {
return rt.err(res, err, n, 'ERR8002');
}
res.api_res = {
record: []
};
return n();
})
.post('/delipmi', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
try {
let query = "delete from ??.?? where `ipmuid` = ?";
let param = [config.db.db11, 'ipmi', arr.data.id];
await tool.promiseQuery(res, query, param);
2017-05-22 09:47:16 +00:00
} catch (err) {
return rt.err(res, err, n, 'ERR8003');
2017-04-13 05:23:08 +00:00
}
2017-05-25 05:37:16 +00:00
res.api_res = {
record: []
};
return n();
})
.post('/getipmiinfo', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.id) return n('ERR0028');
res.api_res = {
record: [],
rt: {
log: [],
sensor: [],
bmcgroup: [],
bmcdata: []
}
};
try {
let query = "select * from ??.?? where `ipmuid` = ?";
let param = [config.db.db11, 'ipmi', arr.data.id];
let ipmi = await tool.promiseQuery(res, query, param);
if (ipmi.data.length == 0) return n('ERR8000');
delete ipmi.data[0].password;
res.api_res.record = tool.checkArray(ipmi.data);
query = "select * from ??.?? order by `guid`";
param = [config.db.db11, 'bmg'];
let bmg = await tool.promiseQuery(res, query, param);
res.api_res.rt.bmcgroup = tool.checkArray(bmg.data);
query = "select * from ??.?? where `ipmuid` = ?";
param = [config.db.db11, 'bmrt', arr.data.id];
let bmrt = await tool.promiseQuery(res, query, param);
res.api_res.rt.bmcdata = tool.checkArray(bmrt.data);
query = "select * from ??.?? where `ipmuid` = ?";
param = [config.db.db11, 'ipmilogrt', arr.data.id];
let log = await tool.promiseQuery(res, query, param);
res.api_res.rt.log = tool.checkArray(log.data);
query = "select * from ??.?? where `ipmuid` = ?";
param = [config.db.db11, 'ipmisenrt', arr.data.id];
let sensor = await tool.promiseQuery(res, query, param);
res.api_res.rt.sensor = tool.checkArray(sensor.data);
} catch (err) {
return rt.err(res, err, n, 'ERR8000');
}
return n();
2017-04-13 05:23:08 +00:00
})
2017-05-26 03:06:15 +00:00
.post('/getbmctl', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
try {
let query = "select `uid`,`name` from ??.??";
let param = [config.db.db11, 'bmctl'];
let ctls = await tool.promiseQuery(res, query, param);
res.api_res = {
record: tool.checkArray(ctls.data)
}
} catch (err) {
return rt.err(res, err, n, 'ERR8000');
}
return n();
})
.post('/runbmctl', async(req, res, n) => {
if (!config.permission.server) return n('ERR9000');
if (!tool.checkPermission(req)) return n('ERR9000');
let arr = req.body;
if (!arr.data) return n('ERR0000');
if (!arr.data.ctlid || !arr.data.ipmuid) return n('ERR0028');
res.api_res = {
record: []
};
try {
let query = "select * from ??.?? where `uid` = ?";
let param = [config.db.db11, 'bmctl', arr.data.ctlid];
let ctl = await tool.promiseQuery(res, query, param);
if (ctl.data.length == 0) return n('ERR8000');
query = "select * from ??.?? where `ipmuid` = ?";
param = [config.db.db11, 'ipmi', arr.data.ipmuid];
let ipmi = await tool.promiseQuery(res, query, param);
if (ipmi.data.length == 0) return n('ERR8000');
let cmd = ctl.data[0].cmd;
cmd = cmd.trim();
if (!cmd) return n('ERR7000');
cmd = cmd.replace(/\@ipmuid/i, ipmi.data[0].ipmuid);
let result = await new Promise((resolve, reject) => {
exec(cmd, (err, stderr, stdout) => {
if (err) return reject(err);
return resolve(stdout || stderr);
});
})
result = result.split(/\n/).filter(e => e).join('');
if (result == 0) return n('ERR7000');
} catch (err) {
return rt.err(res, err, n, 'ERR8000');
}
return n();
})
2017-04-13 05:23:08 +00:00
.all('*', rt.send);
module.exports = router;