forked from skale-me/skale-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskale.js
executable file
·194 lines (168 loc) · 4.99 KB
/
skale.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
// Copyright 2016 Luca-SAS, licensed under the Apache License 2.0
const help=`Usage: skale [options] <command> [<args>]
Create, run, deploy clustered node applications
Commands:
create <app> Create a new application
run [<args>...] Run application
deploy [<args>...] Deploy application (coming soon)
status print status of local skale cluster
stop Stop local skale cluster
Options:
-h, --help Show help
-V, --version Show version
`;
const child_process = require('child_process');
const fs = require('fs');
const net = require('net');
const argv = require('minimist')(process.argv.slice(2), {
string: ['c', 'config', 'H', 'host', 'p', 'port', 'k', 'key'],
boolean: ['h', 'help', 'V', 'version', 's', 'ssl'],
});
var skale_port = 12346;
if (argv.h || argv.help) {
console.log(help);
process.exit();
}
if (argv.V || argv.version) {
var pkg = require('./package');
console.log(pkg.name + '-' + pkg.version);
process.exit();
}
const config = load(argv);
const proto = config.ssl ? require('https') : require('http');
switch (argv._[0]) {
case 'create':
create(argv._[1]);
break;
case 'demo':
run_remote(__dirname + '/demo/' + argv._[1], argv._.splice(2));
break;
case 'run':
run_local(argv._.splice(1));
break;
case 'status':
status_local();
break;
case 'stop':
stop_local_server();
break;
default:
die('Error: invalid command: ' + argv._[0]);
}
function create(name) {
console.log('create application ' + name);
try {
fs.mkdirSync(name);
} catch (error) {
die('skale create error: ' + error.message);
}
process.chdir(name);
const pkg = {
name: name,
version: '0.0.0',
private: true,
keywords: [ 'skale' ],
dependencies: {
'skale-engine': '^0.3.2'
}
};
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2));
var src = `#!/usr/bin/env node
var sc = require('skale-engine').context();
sc.parallelize(['Hello world'])
.collect()
.on('data', console.log)
.on('end', sc.end);
`;
fs.writeFileSync(name + '.js', src);
const npm = child_process.spawnSync('npm', ['install'], {stdio: 'inherit'});
if (npm.status) die('skale create error: npm install failed');
console.log(`Project ${name} is now ready.
Pleas change directory to ${name}: "cd ${name}"
To run your app: "skale run"
To modify your app: edit ${name}.js`)
}
function die(err) {
console.error(help);
console.error(err);
process.exit(1);
}
function load(argv) {
var conf = {}, save = false;
var path = argv.c || argv.config || process.env.SKALE_CONFIG || process.env.HOME + '/.skalerc';
try { conf = JSON.parse(fs.readFileSync(path)); } catch (error) { save = true; }
conf.host = argv.H || argv.host || process.env.SKALE_HOST || conf.host || die('Error: missing host');
conf.port = argv.p || argv.port || process.env.SKALE_PORT || conf.port || die('Error: missing port');
conf.key = argv.k || argv.key || conf.key;
conf.ssl = argv.s || argv.ssl || (conf.ssl ? true : false);
if (save || argv._[0] == 'init') fs.writeFileSync(path, JSON.stringify(conf, null, 2));
return conf;
}
function start_skale(done) {
const out = fs.openSync('skale-server.log', 'a');
const err = fs.openSync('skale-server.log', 'a');
const child = child_process.spawn('node_modules/skale-engine/bin/server.js', ['-l', '2'], {
detached: true,
stdio: ['ignore', out, err]
});
child.unref();
try_connect(5, 1000, done);
}
function try_connect(nb_try, timeout, done) {
const sock = net.connect(skale_port);
sock.on('connect', function () {
sock.end();
done(null);
});
sock.on('error', function (err) {
if (--nb_try <= 0) return done('skale-server not ok');
setTimeout(function () { try_connect(nb_try, timeout, done); }, timeout);
});
}
function stop_local_server() {
const child = child_process.execFile('/usr/bin/pgrep', ['skale-server'], function (err, pid) {
if (! pid) return;
process.kill(pid.trim());
});
}
function status_local() {
const child = child_process.execFile('/bin/ps', ['ux'], function (err, out) {
var lines = out.split(/\r\n|\r|\n/);
for (var i = 0; i < lines.length; i++)
if (i == 0 || lines[i].match(/ skale-/)) console.log(lines[i]);
});
}
function run_local(args) {
const pkg = JSON.parse(fs.readFileSync('package.json'));
var cmd = pkg.name + '.js';
args.splice(0, 0, cmd);
try_connect(0, 0, function (err) {
if (!err) return run_app();
start_skale(run_app);
});
function run_app() { child = child_process.spawn('node', args, {stdio: 'inherit'}); }
}
function run_remote(src, args) {
fs.readFile(src, {encoding: 'utf8'}, function (err, data) {
if (err) throw err;
var postdata = JSON.stringify({src: data, args: args});
var options = {
hostname: config.host,
port: config.port,
path: '/run',
method: 'POST',
headers: {
'X-Auth': config.key,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postdata)
}
};
var req = proto.request(options, function (res) {
res.setEncoding('utf8');
res.pipe(process.stdout);
});
req.on('error', function (err) {throw err;});
req.end(postdata);
});
}