...
|
...
|
@@ -20,24 +20,27 @@ class PageCahe extends Model { |
|
|
async removeCache(keys, storeTableName) {
|
|
|
let self = this;
|
|
|
let servers = await self.findAll();
|
|
|
for (var i = 0; i < servers.length; i++) {
|
|
|
for (let i = 0; i < servers.length; i++) {
|
|
|
try {
|
|
|
await self._removeAllNginxCache(servers[i], keys, storeTableName);
|
|
|
await self._removeCache(servers[i], keys, storeTableName);
|
|
|
} catch (err) {
|
|
|
console.log(err)
|
|
|
}
|
|
|
}
|
|
|
console.log('over')
|
|
|
}
|
|
|
async removeAllCache(storeTableName) {
|
|
|
let self = this;
|
|
|
let servers = await self.findAll();
|
|
|
for (var i = 0; i < servers.length; i++) {
|
|
|
try {
|
|
|
for (let i = 0; i < servers.length; i++) {
|
|
|
let server = servers[i];
|
|
|
try {
|
|
|
//验证两个变量是否为空避免特殊bug造成为空后拼接的shell执行危险操作
|
|
|
if (server.cachepath && storeTableName) {
|
|
|
await self._connStart(async (conn) => {
|
|
|
let scirpt = `rm -rf ${server.cachepath}/${storeTableName}`;
|
|
|
await self._evalScript(conn, scirpt);
|
|
|
//连接ssh后执行操作
|
|
|
return await self._connStart(async (conn) => {
|
|
|
let script = `rm -rf ${server.cachepath}/${storeTableName}`;
|
|
|
return await self._evalScript(conn, script);
|
|
|
}, server);
|
|
|
}
|
|
|
} catch (err) {
|
...
|
...
|
@@ -45,24 +48,26 @@ class PageCahe extends Model { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
async _removeAllNginxCache(server, keys, storeTableName) {
|
|
|
async _removeCache(server, keys, storeTableName) {
|
|
|
let self = this;
|
|
|
let limit = 2;
|
|
|
let block = parseInt(keys.length / limit, 10) + (keys.length % limit ? 1 : 0);
|
|
|
let blockIndex = 0;
|
|
|
//连接ssh后执行操作
|
|
|
await self._connStart(async (conn) => {
|
|
|
for (var i = blockIndex; i < block; i++) {
|
|
|
blockIndex = i;
|
|
|
let items = keys.slice(i * limit, i * limit + 2);
|
|
|
let scirpt = self._joinRemoveScript(server, items, storeTableName);
|
|
|
self._evalScript(conn, scirpt);
|
|
|
let script = self._joinRemoveScript(server, items, storeTableName);
|
|
|
await self._evalScript(conn, script);
|
|
|
}
|
|
|
}, server)
|
|
|
}
|
|
|
_checkDangerScirpt(scirpt) {
|
|
|
if (scirpt.test(/rm -rf \/($| )/)) {
|
|
|
_checkDangerScript(script) {
|
|
|
if (/rm -rf \/($| )/.test(script)) {
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
_connStart(callback, server) {
|
|
|
//ssh掉线重连
|
...
|
...
|
@@ -70,6 +75,7 @@ class PageCahe extends Model { |
|
|
let connStart = (err) => {
|
|
|
let conn = new ssh.Client();
|
|
|
conn.on('ready', async() => {
|
|
|
console.log(`connect ${server.host}`);
|
|
|
await callback(conn)
|
|
|
resolve();
|
|
|
}).on('error', () => {
|
...
|
...
|
@@ -90,19 +96,23 @@ class PageCahe extends Model { |
|
|
connStart(0, 0);
|
|
|
});
|
|
|
}
|
|
|
_evalScript(conn, scirpt) {
|
|
|
_evalScript(conn, script) {
|
|
|
let self = this;
|
|
|
return new Promise((resolve) => {
|
|
|
console.log(scirpt)
|
|
|
return new Promise((resolve, reject) => {
|
|
|
//检查shell脚本的危险操作
|
|
|
if (self._checkDangerScript(script)) {
|
|
|
console.log('_evalScript start');
|
|
|
resolve();
|
|
|
// let result = conn.exec(scirpt, (err, stream) => {
|
|
|
// let result = conn.exec(script, (err, stream) => {
|
|
|
// if (!err) {
|
|
|
// resolve();
|
|
|
// }
|
|
|
// });
|
|
|
})
|
|
|
|
|
|
} else {
|
|
|
reject('检测到危险操作');
|
|
|
}
|
|
|
|
|
|
})
|
|
|
}
|
|
|
_joinRemoveScript(server, keys, storeTableName) {
|
|
|
let script = 'rm';
|
...
|
...
|
|