-
Notifications
You must be signed in to change notification settings - Fork 374
/
scripts.ts
158 lines (137 loc) · 4.11 KB
/
scripts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*!
* Pomelo -- consoleModule runScript
* Copyright(c) 2012 fantasyni <fantasyni@163.com>
* MIT Licensed
*/
import { getLogger } from 'pinus-logger'; let logger = getLogger('pinus-admin', __filename);
import * as monitor from 'pinus-monitor';
import * as vm from 'vm';
import * as fs from 'fs';
import * as util from 'util';
import * as path from 'path';
import { IModule, MonitorCallback, MasterCallback } from '../consoleService';
import { MonitorAgent } from '../monitor/monitorAgent';
import { MasterAgent } from '../master/masterAgent';
export class ScriptsModule implements IModule
{
app: any;
root: string;
commands: {[key:string] : Function};
static moduleId = "scripts";
constructor(opts : {app:string , path:string})
{
this.app = opts.app;
this.root = opts.path;
this.commands = {
'list': list,
'get': get,
'save': save,
'run': run
};
};
monitorHandler(agent : MonitorAgent, msg : any, cb : MonitorCallback)
{
let context = {
app: this.app,
require: require,
os: require('os'),
fs: require('fs'),
process: process,
util: util,
result:undefined as any
};
try
{
vm.createContext(context);
vm.runInNewContext(msg.script, context);
let result = context.result;
if (!result)
{
cb(null, "script result should be assigned to result value to script module context");
} else
{
cb(null, result);
}
} catch (e)
{
cb(null, e.toString());
}
//cb(null, vm.runInContext(msg.script, context));
};
clientHandler(agent : MasterAgent, msg : any, cb : MasterCallback)
{
let fun = this.commands[msg.command];
if (!fun || typeof fun !== 'function')
{
cb('unknown command:' + msg.command);
return;
}
fun(this, agent, msg, cb);
};
}
/**
* List server id and scripts file name
*/
let list = function(scriptModule : ScriptsModule, agent : MasterAgent, msg : any, cb : MasterCallback) {
let servers : string[] = [];
let scripts : string[] = [];
let idMap = agent.idMap;
for (let sid in idMap) {
servers.push(sid);
}
fs.readdir(scriptModule.root, function(err, filenames) {
if (err) {
filenames = [];
}
for (let i = 0, l = filenames.length; i < l; i++) {
scripts.push(filenames[i]);
}
cb(null, {
servers: servers,
scripts: scripts
});
});
};
/**
* Get the content of the script file
*/
let get = function(scriptModule : ScriptsModule, agent : MasterAgent, msg : any, cb : MasterCallback) {
let filename = msg.filename;
if (!filename) {
cb('empty filename');
return;
}
fs.readFile(path.join(scriptModule.root, filename), 'utf-8', function(err, data) {
if (err) {
logger.error('fail to read script file:' + filename + ', ' + err.stack);
cb('fail to read script with name:' + filename);
}
cb(null, data);
});
};
/**
* Save a script file that posted from admin console
*/
let save = function(scriptModule : ScriptsModule, agent : MasterAgent, msg : any, cb : MasterCallback) {
let filepath = path.join(scriptModule.root, msg.filename);
fs.writeFile(filepath, msg.body, function(err) {
if (err) {
logger.error('fail to write script file:' + msg.filename + ', ' + err.stack);
cb('fail to write script file:' + msg.filename);
return;
}
cb();
});
};
/**
* Run the script on the specified server
*/
let run = function(scriptModule : ScriptsModule, agent : MasterAgent, msg : any, cb : MasterCallback) {
agent.request(msg.serverId, ScriptsModule.moduleId, msg, function(err, res) {
if (err) {
logger.error('fail to run script for ' + err.stack);
return;
}
cb(null, res);
});
};