Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

让 console 模块支持自定义 ssh 参数 #936

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/pinus/lib/master/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ export class MasterServer {
if ((autoRestart.toString() === 'true' || restartForce.toString() === 'true') && stopFlags.indexOf(id) < 0) {
let handle = function () {
clearTimeout(pingTimer);
utils.checkPort(server, function (status) {
utils.checkPort(self.app, server, function (status) {
if (status === 'error') {
utils.invokeCallback(cb, new Error('Check port command executed with error.'));
return;
} else if (status === 'busy') {
if (!!server[Constants.RESERVED.RESTART_FORCE]) {
starter.kill([info.pid], [server]);
starter.kill(self.app, [info.pid], [server]);
} else {
utils.invokeCallback(cb, new Error('Port occupied already, check your server to add.'));
return;
Expand Down Expand Up @@ -129,7 +129,7 @@ export class MasterServer {

// monitor servers register event
this.masterConsole.on('register', function (record) {
starter.bindCpu(record.id, record.pid, record.host);
starter.bindCpu(self.app, record.id, record.pid, record.host);
});

this.masterConsole.on('admin-log', function (log, error) {
Expand Down
4 changes: 2 additions & 2 deletions packages/pinus/lib/master/starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function run(app: Application, server: ServerInfo, cb ?: (err?: string |
* @param {String} host server host
* @return {Void}
*/
export function bindCpu(sid: string, pid: string, host: string) {
export function bindCpu(app: Application, sid: string, pid: string, host: string) {
if (os.platform() === Constants.PLATFORM.LINUX && cpus[sid] !== undefined) {
if (utils.isLocal(host)) {
let options: string[] = [];
Expand All @@ -127,7 +127,7 @@ export function bindCpu(sid: string, pid: string, host: string) {
* @param {String} pids array of server's pid
* @param {String} serverIds array of serverId
*/
export function kill(pids: string[], servers: ServerInfo[]) {
export function kill(app: Application, pids: string[], servers: ServerInfo[]) {
let cmd;
for (let i = 0; i < servers.length; i++) {
let server = servers[i];
Expand Down
37 changes: 2 additions & 35 deletions packages/pinus/lib/modules/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,39 +303,6 @@ let blacklist = function (app: Application, agent: MasterAgent, msg: any, cb: Ma
});
};

let checkPort = function (server: ServerInfo, cb: MasterCallback) {
if (!server.port && !server.clientPort) {
utils.invokeCallback(cb, 'leisure');
return;
}

let p = server.port || server.clientPort;
let host = server.host;
// let cmd = 'netstat -tln | grep ';
let cmd = os.type() === 'Windows_NT' ?
`netstat -ano | %windir%\\system32\\find.exe ` : `netstat -tln | grep `;
if (!utils.isLocal(host)) {
cmd = 'ssh ' + host + ' ' + cmd;
}
p = os.type() === 'Windows_NT' ? `"${p}"` as any : p
exec(cmd + p, function (err, stdout, stderr) {
if (stdout || stderr) {
logger.debug('checkport has error?', cmd + p, 'stdout:', stdout, 'stderr', stderr)
utils.invokeCallback(cb, 'busy');
} else {
p = server.clientPort;
p = os.type() === 'Windows_NT' ? `"${p}"` as any : p
exec(cmd + p, function (err, stdout, stderr) {
if (stdout || stderr) {
utils.invokeCallback(cb, 'busy');
} else {
utils.invokeCallback(cb, 'leisure');
}
});
}
});
};

let parseArgs = function (msg: any, info: any, cb: (err?: string | Error, data?: any) => void) {
let rs: { [key: string]: string } = {};
let args = msg.args;
Expand Down Expand Up @@ -380,7 +347,7 @@ let startServer = function (app: Application, msg: any, cb: (err?: Error | strin
};

let runServer = function (app: Application, server: ServerInfo, cb: (err?: Error, result?: any) => void) {
checkPort(server, function (status) {
utils.checkPort(app, server, function (status) {
if (status === 'busy') {
utils.invokeCallback(cb, new Error('Port occupied already, check your server to add.'));
} else {
Expand Down Expand Up @@ -419,7 +386,7 @@ let startCluster = function (app: Application, msg: any, cb: MasterCallback) {

let start = function (server: ServerInfo) {
return (function () {
checkPort(server, function (status) {
utils.checkPort(app, server, function (status) {
if (status === 'busy') {
fails.push(server);
latch.done();
Expand Down
18 changes: 9 additions & 9 deletions packages/pinus/lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import * as util from 'util';
import {exec} from 'child_process';
import {getLogger} from 'pinus-logger';
import * as Constants from './constants';
import {pinus} from '../pinus';
import {ServerInfo} from './constants';
import {Application} from '../application';
import * as path from 'path';
import { pinus } from '../pinus';

let logger = getLogger('pinus', path.basename(__filename));

Expand Down Expand Up @@ -202,16 +202,16 @@ export function ping(host: string, cb: (ret: boolean) => void) {
* Check if server is exsit.
*
*/
export function checkPort(server: ServerInfo, cb: (result: string) => void) {
export function checkPort(app: Application, server: ServerInfo, cb: (result: string) => void) {
if (!server.port && !server.clientPort) {
invokeCallback(cb, 'leisure');
return;
}
let port = server.port || server.clientPort;
let host = server.host;
let generateCommand = function (host: string, port: number) {
const host = server.host;
const generateCommand = function (host: string, port: number) {
let cmd;
let ssh_params = pinus.app.get(Constants.RESERVED.SSH_CONFIG_PARAMS);
let ssh_params = app.get(Constants.RESERVED.SSH_CONFIG_PARAMS);
if (!!ssh_params && Array.isArray(ssh_params)) {
ssh_params = ssh_params.join(' ');
}
Expand All @@ -225,16 +225,16 @@ export function checkPort(server: ServerInfo, cb: (result: string) => void) {
}
return cmd;
};
let cmd1 = generateCommand(host, port);
let child = exec(cmd1, function (err, stdout, stderr) {
const cmd1 = generateCommand(host, port);
exec(cmd1, function (err, stdout, stderr) {
if (err) {
logger.error('command %s execute with error: %j', cmd1, err.stack);
invokeCallback(cb, 'error');
} else if (stdout.trim() !== '0') {
invokeCallback(cb, 'busy');
} else {
port = server.clientPort;
let cmd2 = generateCommand(host, port);
const cmd2 = generateCommand(host, port);
exec(cmd2, function (err, stdout, stderr) {
if (err) {
logger.error('command %s execute with error: %j', cmd2, err.stack);
Expand All @@ -250,7 +250,7 @@ export function checkPort(server: ServerInfo, cb: (result: string) => void) {
}

export function isLocal(host: string) {
let app = pinus.app;
const app = pinus.app;
if (!app) {
return host === '127.0.0.1' || host === 'localhost' || host === '0.0.0.0' || inLocal(host);
} else {
Expand Down
Loading