-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
274 lines (256 loc) · 8.14 KB
/
index.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (c) 2019-2022, Taegus Cromis, The Conceal Developers
//
// Please see the included LICENSE file for more information.
import { getNodeActualPath } from "./units/utils.js";
import commandLineUsage from "command-line-usage";
import commandLineArgs from "command-line-args";
import { NodeGuard } from "./units/engine.js";
import { Initialize } from "./units/setup.js";
import path from "path";
import fs from "fs";
import {
downloadLatestDaemon,
downloadLatestGuardian
} from "./units/download.js";
import {
install,
remove,
stop,
start,
status
} from "./units/service.js";
// read the package.json to have version info available
const pjson = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json")));
try {
var cmdOptions = commandLineArgs([{
name: "config",
type: String
}, {
name: "daemon",
type: String
}, {
name: "service",
type: String
}, {
name: "setup",
type: Boolean
}, {
name: "node",
type: String
}, {
name: "update",
type: Boolean
}, {
name: "version",
type: Boolean
}, {
name: "help",
alias: "h",
type: Boolean
}]);
} catch (err) {
console.error("\nUknown command line parameter. Use --help for instructions.");
process.exit();
}
if (cmdOptions.help) {
const sections = [
{
header: 'Conceal Node Guardian',
content: 'This is a guardian app for the conceal node daemon. Handles restarts, sends notifications, registers to pool...'
},
{
header: 'Options',
optionList: [
{
name: 'config',
typeLabel: '{underline file}',
description: 'The path to configuration file. If empty it uses the config.json in the same directory as the app.'
},
{
name: 'daemon',
typeLabel: '{underline file}',
description: 'The path to node daemon executable. If empty it uses the same directory as the app.'
},
{
name: 'setup',
description: 'Initiates the interactive config setup for the guardian'
},
{
name: 'service',
description: 'Controls the service behaviour. Possible values are: "install", "remove", "start", "stop"'
},
{
name: 'node',
description: 'Node related commands. Possible values are: "update"'
},
{
name: 'update',
description: 'Update the guardian instance to the latest version'
},
{
name: 'version',
description: 'Shows the verion of the Guardian app'
},
{
name: 'help',
description: 'Shows this help instructions'
}
]
},
{
header: 'Service option values',
optionList: [
{
name: 'install',
description: 'Install the guardian as a service in the OS.'
},
{
name: 'remove',
description: 'Removes the guardian as a service from the OS.'
},
{
name: 'start',
description: 'Starts the guardian as OS service.'
},
{
name: 'stop',
description: 'Stops the guardian as OS service.'
},
{
name: 'status',
description: 'Shows the status of the OS service.'
}
]
},
{
header: 'Node option values',
optionList: [
{
name: 'update',
description: 'Updates to the latest stable version of the node daemon. Node must be stoped first'
}
]
}
];
const usage = commandLineUsage(sections);
console.log(usage);
} else if (cmdOptions.version) {
console.log(`\nConceal node guardian version ${pjson.version}\n`);
} else {
const rootPath = process.cwd();
const configFileName = cmdOptions.config || path.join(rootPath, "config.json");
if (!fs.existsSync(configFileName)) {
console.log(`\n"${configFileName}" does not exist! Specify the correct path to the config file or create config.json in the same directory as the application. You can use the config.json.sample as an example\n`);
} else {
// read config option to use them either in engine or setup module
const configOpts = JSON.parse(fs.readFileSync(cmdOptions.config || path.join(rootPath, "config.json"), "utf8"));
// check if arguments are specified if not make an empty array
if (!(configOpts.node && configOpts.node.args)) {
configOpts.node.args = [];
}
if (configOpts.node && configOpts.node.bindAddr) {
var addrIndex = configOpts.node.args.indexOf("--rpc-bind-ip");
if (addrIndex == -1) {
// add bind address to arguments
configOpts.node.args.push("--rpc-bind-ip");
configOpts.node.args.push(configOpts.node.bindAddr);
} else {
configOpts.node.args[addrIndex + 1] = configOpts.node.bindAddr;
}
}
if (configOpts.node && configOpts.node.port) {
var portIndex = configOpts.node.args.indexOf("--rpc-bind-port");
if (portIndex == -1) {
// add fee address to arguments
configOpts.node.args.push("--rpc-bind-port");
configOpts.node.args.push(configOpts.node.port);
} else {
configOpts.node.args[portIndex + 1] = configOpts.node.port;
}
}
if (configOpts.node && configOpts.node.feeAddr) {
// add fee address to arguments
configOpts.node.args.push("--fee-address");
configOpts.node.args.push(configOpts.node.feeAddr);
}
if (cmdOptions.setup) {
Initialize(configFileName);
} else if (cmdOptions.service) {
switch (cmdOptions.service) {
case "install":
install(configOpts, configFileName);
break;
case "remove":
remove(configOpts, configFileName);
break;
case "start":
start(configOpts, configFileName);
break;
case "stop":
stop(configOpts, configFileName);
break;
case "status":
status(configOpts, configFileName);
break;
default: console.log('\nWrong parameter for service command. Valid values: "install", "remove", "start", "stop", "status"\n');
}
} else if (cmdOptions.node) {
if (cmdOptions.node === "update") {
stop(configOpts, configFileName);
downloadLatestDaemon(getNodeActualPath(cmdOptions, configOpts, rootPath), function (error) {
if (error) {
console.log(`\nError updating daemon: ${error}\n`);
} else {
console.log("\nThe daemon has been succesfully updated\n");
}
});
} else {
console.log('\nWrong parameter for node command. Valid values: "update"\n');
}
} else if (cmdOptions.update) {
stop(configOpts, configFileName);
downloadLatestGuardian(function (error) {
if (error) {
console.log(`\nError updating the guardian: ${error}\n`);
} else {
console.log("\nThe guardian has been succesfully updated\n");
}
});
} else {
const nodePath = getNodeActualPath(cmdOptions, configOpts, rootPath);
var guardInstance = null;
// createGuardInstance function
var createGuardInstance = function () {
guardInstance = new NodeGuard(cmdOptions, configOpts, rootPath, pjson.version);
};
if (!fs.existsSync(nodePath)) {
downloadLatestDaemon(nodePath, function (error) {
if (error) {
console.log(`\nError updating daemon: ${error}\n`);
} else {
console.log("\nThe daemon has been succesfully updated\n");
createGuardInstance();
}
});
} else {
createGuardInstance();
}
process.on('uncaughtException', function (err) {
guardInstance.logError(err);
});
process.on('SIGINT', function() {
console.log("Stopping the guardian....");
if (guardInstance.getProcess()) {
guardInstance.getProcess().on("exit", function (code, signal) {
console.log("Daemon stopped, exiting....");
process.exit();
});
// stop the daemon process
guardInstance.stop(false);
} else {
process.exit();
}
});
}
}
}