-
Notifications
You must be signed in to change notification settings - Fork 121
/
SeededPodInitializer.ts
57 lines (48 loc) · 1.98 KB
/
SeededPodInitializer.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
import { readJson } from 'fs-extra';
import type { RegistrationManager } from '../identity/interaction/email-password/util/RegistrationManager';
import { getLoggerFor } from '../logging/LogUtil';
import { createErrorMessage } from '../util/errors/ErrorUtil';
import { Initializer } from './Initializer';
/**
* Uses a {@link RegistrationManager} to initialize accounts and pods
* for all seeded pods. Reads the pod settings from seededPodConfigJson.
*/
export class SeededPodInitializer extends Initializer {
protected readonly logger = getLoggerFor(this);
private readonly registrationManager: RegistrationManager;
private readonly configFilePath: string | null;
public constructor(registrationManager: RegistrationManager, configFilePath: string | null) {
super();
this.registrationManager = registrationManager;
this.configFilePath = configFilePath;
}
public async handle(): Promise<void> {
if (!this.configFilePath) {
return;
}
const configuration = await readJson(this.configFilePath, 'utf8');
let count = 0;
for await (const input of configuration) {
const config = {
confirmPassword: input.password,
createPod: true,
createWebId: true,
register: true,
...input,
};
this.logger.info(`Initializing pod ${input.podName}`);
// Validate the input JSON
const validated = this.registrationManager.validateInput(config, true);
this.logger.debug(`Validated input: ${JSON.stringify(validated)}`);
// Register and/or create a pod as requested. Potentially does nothing if all booleans are false.
try {
await this.registrationManager.register(validated, true);
this.logger.info(`Initialized seeded pod and account for "${input.podName}".`);
count += 1;
} catch (error: unknown) {
this.logger.warn(`Error while initializing seeded pod: ${createErrorMessage(error)})}`);
}
}
this.logger.info(`Initialized ${count} seeded pods.`);
}
}