-
Notifications
You must be signed in to change notification settings - Fork 9
/
gui-commands.ts
216 lines (196 loc) · 6.06 KB
/
gui-commands.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
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
import pm2 from 'pm2';
import {Command} from 'commander';
import path = require('path');
import {timingSafeEqual} from 'crypto';
import {Pm2ProcessStatus, statusFromPM2} from './pm2';
import merge from 'deepmerge';
import {defaultGuiConfig, guiConfigType, guiConfigSchema} from './config/default-gui-config';
import fs from 'fs';
import * as yaml from 'js-yaml';
import * as cryptoShardus from '@shardus/crypto-utils';
import {getInstalledGuiVersion} from './utils/project-data';
import {File} from './utils'
import crypto from 'crypto';
import Ajv from "ajv"
let config = defaultGuiConfig;
const validateGuiConfig = new Ajv().compile(guiConfigSchema)
cryptoShardus.init('64f152869ca2d473e4ba64ab53f49ccdb2edae22da192c126850970e788af347');
function isNumber(n: string) {
const parsedN = parseInt(n);
return !isNaN(parsedN) && isFinite(parsedN);
}
const guiConfigPath = path.join(__dirname, `../${File.GUI_CONFIG}`)
if (fs.existsSync(guiConfigPath)) { // eslint-disable-line security/detect-non-literal-fs-filename
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fileConfig = JSON.parse(fs.readFileSync(guiConfigPath).toString())
if (validateGuiConfig(fileConfig)) {
config = merge(config, fileConfig as guiConfigType, {arrayMerge: (target, source) => source})
// `as guiConfigType` above is valid because validateGuiConfig() passed
} else {
console.warn(`warning: config has been ignored due to invalid JSON schema:`)
console.warn(`${guiConfigPath}`)
}
}
export function registerGuiCommands(program: Command) {
const gui = program.command('gui').description('GUI related commands');
gui
.command('status')
.description(
'Show if GUI is running or not; also the port and URL to connect to it'
)
.action(() => {
pm2.describe('operator-gui', (err, descriptions) => {
if (err) {
console.error(err);
return pm2.disconnect();
}
if (descriptions.length === 0) {
console.log('operator gui not running!');
return pm2.disconnect();
}
const description = descriptions[0];
const status: Pm2ProcessStatus = statusFromPM2(description);
status.link = `https://localhost:${config.gui.port}/`;
console.log(yaml.dump(status));
return pm2.disconnect();
});
});
gui
.command('start')
.description('Starts the GUI server')
.action(async () => await startGui());
gui
.command('restart')
.description('Restarts the GUI server')
.action(async () => {
await stopGui();
await startGui();
});
gui
.command('version')
.description('Show the GUI version')
.action(() => {
console.log(getInstalledGuiVersion());
});
gui
.command('stop')
.description('Stops the GUI server')
.action(async () => {
await stopGui();
});
const setCommand = gui
.command('set')
.description('command to set various config parameters');
setCommand
.command('port')
.arguments('<port>')
.description('Set the GUI server port')
.action(port => {
if (!isNumber(port)) {
console.error("Port is not a number");
return;
}
port = parseInt(port);
if(port < 1024) {
console.error("Port is reserved");
return;
}
config.gui.port = parseInt(port);
process.env.DASHPORT = port; // set the DASHPORT environment variable
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.writeFile(
path.join(__dirname, `../${File.GUI_CONFIG}`),
JSON.stringify(config, undefined, 2),
err => {
if (err) console.log(err);
}
);
});
setCommand
.command('password')
.arguments('<password>')
.description('Set the GUI server password')
.option('-h', 'Changes how the password is hashed. For internal use only')
.action((password, options) => {
if(!options.h) {
password = crypto.createHash('sha256').update(password).digest('hex');
}
config.gui.pass = cryptoShardus.hash(password);
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.writeFile(
path.join(__dirname, `../${File.GUI_CONFIG}`),
JSON.stringify(config, undefined, 2),
err => {
if (err) console.error(err);
}
);
});
gui
.command('login')
.arguments('<password>')
.description('verify GUI password')
.action(password => {
if (
!timingSafeEqual(Buffer.from(password), Buffer.from(config.gui.pass))
) {
console.log(yaml.dump({login: 'unauthorized'}));
return;
}
console.log(yaml.dump({login: 'authorized'}));
});
function startGui() {
// Exec PM2 to start the GUI server
return new Promise<void>((resolve, reject) =>
pm2.connect(err => {
if (err) {
console.error(err);
reject('Unable to connect to PM2');
}
// Start next.js front end on port 3000
pm2.start(
{
name: 'operator-gui',
cwd: `${path.join(__dirname, '../../../gui')}`,
script: 'npm',
args: 'start',
env: {PORT: `${config.gui.port}`},
},
err => {
if (err) {
console.error(err);
reject('Unable to start GUI');
}
pm2.disconnect();
resolve();
}
);
})
);
}
function stopGui() {
return new Promise<void>((resolve, reject) => {
pm2.connect(err => {
if (err) {
console.error(err);
reject('Unable to connect to PM2');
}
pm2.stop('operator-gui', err => {
if (err) {
console.log(err);
reject('Unable to stop gui');
}
pm2.disconnect();
resolve();
});
pm2.delete('operator-gui', err => {
if (err) {
console.log(err);
reject('Unable to delete gui');
}
pm2.disconnect();
resolve();
});
});
});
}
}