This repository was archived by the owner on Jul 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstart.ts
300 lines (264 loc) · 8.96 KB
/
start.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { Environment, Environments } from '@mockoon/commons';
import { Command, flags } from '@oclif/command';
import { readFile as readJSONFile } from 'jsonfile';
import { join, resolve } from 'path';
import { Proc, ProcessDescription } from 'pm2';
import { format } from 'util';
import { Config } from '../config';
import { commonFlags, startFlags } from '../constants/command.constants';
import { Messages } from '../constants/messages.constants';
import { parseDataFiles, prepareEnvironment } from '../libs/data';
import { ProcessListManager, ProcessManager } from '../libs/process-manager';
import { createServer } from '../libs/server';
import { getDirname, portInUse, portIsValid } from '../libs/utils';
interface EnvironmentInfo {
name: string;
protocol: string;
hostname: string;
port: number;
endpointPrefix: string;
dataFile: string;
initialDataDir?: string | null;
logTransaction?: boolean;
}
type StartFlags = {
pname: string[];
hostname: string[];
'daemon-off': boolean;
container: boolean;
data: string[];
port: number[];
'log-transaction': boolean;
repair: boolean;
help: void;
};
export default class Start extends Command {
public static description = 'Start one or more mock API';
public static examples = [
'$ mockoon-cli start --data ~/data.json',
'$ mockoon-cli start --data ~/data1.json ~/data2.json --port 3000 3001 --pname mock1 mock2',
'$ mockoon-cli start --data https://file-server/data.json',
'$ mockoon-cli start --data ~/data.json --pname "proc1"',
'$ mockoon-cli start --data ~/data.json --daemon-off',
'$ mockoon-cli start --data ~/data.json --log-transaction'
];
public static flags = {
...commonFlags,
...startFlags,
pname: flags.string({
char: 'N',
description: 'Override the process(es) name(s)',
multiple: true,
default: []
}),
hostname: flags.string({
char: 'l',
description: 'Listening hostname(s)',
multiple: true,
default: []
}),
'daemon-off': flags.boolean({
char: 'D',
description:
'Keep the CLI in the foreground and do not manage the process with PM2',
default: false
}),
/**
* /!\ Undocumented flag.
* Mostly for internal use when `start `command is called during
* a Docker image build.
*
* When using the `dockerize` command, file loading, validity checks,
* migrations, etc. are all performed, and the single environment is
* extracted in a separated file next to the generated Dockerfile.
* It's easier to directly provide this file to the `start` command run
* from the Dockerfile when building the Docker image rather than
* having the image build failing due to a failure in the `start` command.
*/
container: flags.boolean({
char: 'c',
hidden: true
})
};
public async run(): Promise<void> {
const { flags: userFlags } = this.parse(Start);
let environmentsInfo: EnvironmentInfo[] = [];
try {
// We are in a container, env file is ready and relative to the Dockerfile
if (userFlags.container) {
environmentsInfo = await this.getEnvInfoListFromContainerFlag(
userFlags
);
} else {
const parsedEnvironments = await parseDataFiles(userFlags.data);
userFlags.data = parsedEnvironments.filePaths;
environmentsInfo = await this.getEnvironmentsInfo(
userFlags,
parsedEnvironments.environments
);
}
for (const environmentInfo of environmentsInfo) {
await this.validatePort(environmentInfo.port, environmentInfo.hostname);
await this.validateName(environmentInfo.name);
if (userFlags['daemon-off']) {
this.startForegroundProcess(environmentInfo);
} else {
await this.startManagedProcess(environmentInfo);
}
}
} catch (error: any) {
this.error(error.message);
} finally {
ProcessManager.disconnect();
}
}
private async addProcessToProcessListManager(
environmentInfo: EnvironmentInfo,
process: Proc
): Promise<void> {
await ProcessListManager.addProcess({
name: environmentInfo.name,
port: environmentInfo.port,
hostname: environmentInfo.hostname,
endpointPrefix: environmentInfo.endpointPrefix,
pid: process[0].pm2_env.pm_id
});
}
private logStartedProcess(environmentInfo: EnvironmentInfo, process: Proc) {
const hostname =
environmentInfo.hostname === '0.0.0.0'
? 'localhost'
: environmentInfo.hostname;
this.log(
Messages.CLI.PROCESS_STARTED,
environmentInfo.protocol,
hostname,
environmentInfo.port,
process[0].pm2_env.pm_id,
process[0].pm2_env.name
);
}
/**
* Start the mock server and run it in the same process in the foreground.
* We don't use PM2 here to manage the process
*
* @param environmentInfo
* @returns
*/
private startForegroundProcess(environmentInfo: EnvironmentInfo): void {
const parameters: Parameters<typeof createServer>[0] = {
data: environmentInfo.dataFile,
environmentDir: environmentInfo.initialDataDir
? environmentInfo.initialDataDir
: '',
logTransaction: environmentInfo.logTransaction,
fileTransportsOptions: [
{ filename: join(Config.logsPath, `${environmentInfo.name}-out.log`) }
]
};
createServer(parameters);
}
/**
* Start the mock server and manage the process with PM2
*
* @param environmentInfo
* @returns
*/
private async startManagedProcess(environmentInfo: EnvironmentInfo) {
const args = ['--data', environmentInfo.dataFile];
if (environmentInfo.initialDataDir) {
args.push('--environmentDir', environmentInfo.initialDataDir);
}
if (environmentInfo.logTransaction) {
args.push('--logTransaction');
}
const process = await ProcessManager.start({
max_restarts: 1,
wait_ready: true,
min_uptime: 10000,
kill_timeout: 2000,
args,
error: join(Config.logsPath, `${environmentInfo.name}-error.log`),
output: join(Config.logsPath, `${environmentInfo.name}-out.log`),
name: environmentInfo.name,
script: resolve(__dirname, '../libs/server-start-script.js'),
exec_mode: 'fork'
});
if (process[0].pm2_env.status === 'errored') {
// if process is errored we want to delete it
await this.handleProcessError(environmentInfo.name);
}
this.logStartedProcess(environmentInfo, process);
await this.addProcessToProcessListManager(environmentInfo, process);
}
private async handleProcessError(name: string) {
// if process is errored we want to delete it
await ProcessManager.delete(name);
this.error(format(Messages.CLI.PROCESS_START_LOG_ERROR, name, name));
}
private async getEnvInfoListFromContainerFlag(
userFlags: StartFlags
): Promise<EnvironmentInfo[]> {
const environmentsInfo: EnvironmentInfo[] = [];
for (const dataFile of userFlags.data) {
const environment: Environment = await readJSONFile(dataFile, 'utf-8');
let protocol = 'http';
if (environment.tlsOptions.enabled) {
protocol = 'https';
}
environmentsInfo.push({
protocol,
dataFile,
name: environment.name,
hostname: environment.hostname,
port: environment.port,
endpointPrefix: environment.endpointPrefix,
initialDataDir: null,
logTransaction: userFlags['log-transaction']
});
}
return environmentsInfo;
}
private async getEnvironmentsInfo(
userFlags: StartFlags,
environments: Environments
): Promise<EnvironmentInfo[]> {
const environmentsInfo: EnvironmentInfo[] = [];
for (let envIndex = 0; envIndex < environments.length; envIndex++) {
try {
const environmentInfo = await prepareEnvironment({
environment: environments[envIndex],
userOptions: {
hostname: userFlags.hostname[envIndex],
pname: userFlags.pname[envIndex],
port: userFlags.port[envIndex]
},
repair: userFlags.repair
});
environmentsInfo.push({
...environmentInfo,
initialDataDir: getDirname(userFlags.data[envIndex]),
logTransaction: userFlags['log-transaction']
});
} catch (error: any) {
this.error(error.message);
}
}
return environmentsInfo;
}
private async validateName(name: string) {
const runningProcesses: ProcessDescription[] = await ProcessManager.list();
const processNamesList = runningProcesses.map((process) => process.name);
if (processNamesList.includes(name)) {
this.error(format(Messages.CLI.PROCESS_NAME_USED_ERROR, name));
}
}
private async validatePort(port: number, hostname: string) {
if (!portIsValid(port)) {
this.error(format(Messages.CLI.PORT_IS_NOT_VALID, port));
}
if (await portInUse(port, hostname)) {
this.error(format(Messages.CLI.PORT_ALREADY_USED, port));
}
}
}