@git.zone/cli 7.4.0 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/mod_release/helpers.npmpackages.d.ts +5 -3
- package/dist_ts/mod_release/helpers.npmpackages.js +6 -4
- package/dist_ts/mod_services/classes.dockercontainer.d.ts +13 -2
- package/dist_ts/mod_services/classes.dockercontainer.js +40 -40
- package/dist_ts/mod_services/classes.globalregistry.d.ts +12 -0
- package/dist_ts/mod_services/classes.globalregistry.js +96 -13
- package/dist_ts/mod_services/classes.globalregistrystore.d.ts +7 -1
- package/dist_ts/mod_services/classes.globalregistrystore.js +6 -6
- package/dist_ts/mod_services/classes.legacymongomigration.d.ts +239 -0
- package/dist_ts/mod_services/classes.legacymongomigration.js +970 -0
- package/dist_ts/mod_services/classes.serviceconfiguration.d.ts +18 -8
- package/dist_ts/mod_services/classes.serviceconfiguration.js +47 -35
- package/dist_ts/mod_services/classes.servicedatamarker.d.ts +42 -1
- package/dist_ts/mod_services/classes.servicedatamarker.js +101 -8
- package/dist_ts/mod_services/classes.servicemanager.d.ts +99 -33
- package/dist_ts/mod_services/classes.servicemanager.js +652 -384
- package/dist_ts/mod_services/classes.servicepruner.js +27 -20
- package/dist_ts/mod_services/index.js +51 -7
- package/dist_ts/mod_services/mod.plugins.d.ts +2 -1
- package/dist_ts/mod_services/mod.plugins.js +4 -2
- package/dist_ts_migration/002.runtime-config.d.ts +1 -1
- package/dist_ts_migration/002.runtime-config.js +8 -2
- package/dist_ts_migration/003.service-data-marker.js +125 -30
- package/dist_ts_migration/004.global-registry.js +30 -8
- package/package.json +2 -2
- package/readme.md +127 -28
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/mod_release/helpers.npmpackages.ts +5 -3
- package/ts/mod_services/classes.dockercontainer.ts +58 -50
- package/ts/mod_services/classes.globalregistry.ts +115 -14
- package/ts/mod_services/classes.globalregistrystore.ts +16 -6
- package/ts/mod_services/classes.legacymongomigration.ts +1308 -0
- package/ts/mod_services/classes.serviceconfiguration.ts +64 -40
- package/ts/mod_services/classes.servicedatamarker.ts +149 -8
- package/ts/mod_services/classes.servicemanager.ts +877 -454
- package/ts/mod_services/classes.servicepruner.ts +27 -19
- package/ts/mod_services/index.ts +67 -6
- package/ts/mod_services/mod.plugins.ts +4 -1
- package/ts_migration/002.runtime-config.ts +8 -1
- package/ts_migration/003.service-data-marker.ts +168 -29
- package/ts_migration/004.global-registry.ts +38 -7
|
@@ -7,17 +7,17 @@ import {
|
|
|
7
7
|
} from './classes.serviceconfiguration.js';
|
|
8
8
|
import {
|
|
9
9
|
DockerContainer,
|
|
10
|
-
matchDockerUlimits,
|
|
11
10
|
type IContainerInspectInfo,
|
|
12
|
-
type TDockerUlimits,
|
|
13
11
|
} from './classes.dockercontainer.js';
|
|
14
12
|
import { GlobalRegistry } from './classes.globalregistry.js';
|
|
15
13
|
import {
|
|
16
14
|
getLegacyMinioDataDirectory,
|
|
15
|
+
getLegacyMongoDataDirectory,
|
|
17
16
|
getServiceContainerLabels,
|
|
18
17
|
getServiceDataDirectory,
|
|
19
18
|
hasExactServiceContainerLabels,
|
|
20
19
|
markerProvesServiceDataDirectory,
|
|
20
|
+
noSqlDbImage,
|
|
21
21
|
objectStorageImage,
|
|
22
22
|
readServiceDataMarker,
|
|
23
23
|
recordServiceDataDirectory,
|
|
@@ -30,6 +30,10 @@ import {
|
|
|
30
30
|
} from './classes.servicedatamarker.js';
|
|
31
31
|
import { readPersistedServiceConfiguration } from './classes.servicenames.js';
|
|
32
32
|
import { withServiceDataOperationLock } from './classes.interprocesslock.js';
|
|
33
|
+
import {
|
|
34
|
+
LegacyMongoMigration,
|
|
35
|
+
getLegacyMongoMigrationEvidenceRoot,
|
|
36
|
+
} from './classes.legacymongomigration.js';
|
|
33
37
|
import {
|
|
34
38
|
runServiceMigrations,
|
|
35
39
|
type TServiceMigrationScope,
|
|
@@ -69,6 +73,7 @@ export type TServiceManagerOperation =
|
|
|
69
73
|
| 'compass'
|
|
70
74
|
| 'config'
|
|
71
75
|
| 'logs'
|
|
76
|
+
| 'migrate'
|
|
72
77
|
| 'remove'
|
|
73
78
|
| 'reconfigure'
|
|
74
79
|
| 'restart'
|
|
@@ -82,7 +87,7 @@ export const getServiceManagerMigrationScopes = (
|
|
|
82
87
|
'global-registry',
|
|
83
88
|
'service-selection',
|
|
84
89
|
'runtime-config',
|
|
85
|
-
...(['start', 'restart', 'reconfigure', 'clean'].includes(operationArg)
|
|
90
|
+
...(['start', 'restart', 'reconfigure', 'clean', 'migrate'].includes(operationArg)
|
|
86
91
|
? (['service-data-marker'] as const)
|
|
87
92
|
: []),
|
|
88
93
|
];
|
|
@@ -102,16 +107,87 @@ export interface IServicesStatus {
|
|
|
102
107
|
minioContainer?: string;
|
|
103
108
|
minioDataDirectory: string;
|
|
104
109
|
minioDataExists: boolean;
|
|
110
|
+
/** Unmigrated legacy mongod container, when one still exists. */
|
|
111
|
+
mongoContainer?: string;
|
|
112
|
+
/** Preserved mongod data; migrated or not, GitZone never deletes it. */
|
|
113
|
+
mongoDataDirectory: string;
|
|
114
|
+
mongoDataExists: boolean;
|
|
115
|
+
/** Migration evidence (dumps, inventories, logs); never deleted by GitZone. */
|
|
116
|
+
mongoMigrationDirectory: string;
|
|
117
|
+
mongoMigrationDirectoryExists: boolean;
|
|
105
118
|
};
|
|
106
119
|
totalDataBytes: number;
|
|
107
120
|
}
|
|
108
121
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
122
|
+
/** Wire-protocol port inside the NoSQLDB container; published as MONGODB_PORT. */
|
|
123
|
+
export const noSqlDbWirePort = '27017';
|
|
124
|
+
/** Management UI port inside the container; deliberately never published. */
|
|
125
|
+
export const noSqlDbUiPort = '3000';
|
|
126
|
+
/*
|
|
127
|
+
* Contract of the pinned `noSqlDbImage` (lossless.zone/database 2.0.0), as
|
|
128
|
+
* reported by its image config. A created container must carry exactly these
|
|
129
|
+
* values; any drift means the container was not created by this release.
|
|
130
|
+
*/
|
|
131
|
+
/** Numeric `uid:gid` of the image's `database` user; also owns the bind-mounted data. */
|
|
132
|
+
export const noSqlDbContainerUser = '999:999';
|
|
133
|
+
export const noSqlDbExpectedEntrypoint = ['/sbin/tini', '--'];
|
|
134
|
+
export const noSqlDbExpectedCommand = ['deno', 'run', '--allow-all', 'mod.ts', 'server'];
|
|
135
|
+
/** The image's own HEALTHCHECK, compared exactly. */
|
|
136
|
+
export const noSqlDbExpectedHealthcheck = {
|
|
137
|
+
test: ['CMD-SHELL', 'wget -q -O /dev/null http://127.0.0.1:3000/readyz || exit 1'],
|
|
138
|
+
interval: 30_000_000_000,
|
|
139
|
+
timeout: 5_000_000_000,
|
|
140
|
+
startPeriod: 10_000_000_000,
|
|
141
|
+
retries: 3,
|
|
142
|
+
};
|
|
143
|
+
export const noSqlDbReconciliationTimeoutMs = 60_000;
|
|
144
|
+
/** Seconds the restore-phase NoSQLDB gets to flush its data before Docker kills it. */
|
|
145
|
+
const noSqlDbMigrationStopGraceSeconds = 60;
|
|
146
|
+
/** `service`: published and auto-restarting. `migration`: unreachable restore phase. */
|
|
147
|
+
type TNoSqlDbContainerShape = 'service' | 'migration';
|
|
148
|
+
type THardenedService = 'mongodb' | 'objectstorage';
|
|
149
|
+
|
|
150
|
+
/** Image and in-container user that own each hardened data directory. */
|
|
151
|
+
const hardenedServiceSpecs: Record<
|
|
152
|
+
THardenedService,
|
|
153
|
+
{
|
|
154
|
+
label: string;
|
|
155
|
+
image: string;
|
|
156
|
+
/** `user:group` the data directory is handed to, as the image resolves it. */
|
|
157
|
+
dataOwner: string;
|
|
158
|
+
recoverablePredecessors: Array<{ entrypoint: string; command: string[] }>;
|
|
159
|
+
}
|
|
160
|
+
> = {
|
|
161
|
+
mongodb: {
|
|
162
|
+
label: 'NoSQLDB',
|
|
163
|
+
image: noSqlDbImage,
|
|
164
|
+
dataOwner: noSqlDbContainerUser,
|
|
165
|
+
recoverablePredecessors: [],
|
|
113
166
|
},
|
|
114
|
-
|
|
167
|
+
objectstorage: {
|
|
168
|
+
label: 'ObjectStorage',
|
|
169
|
+
image: objectStorageImage,
|
|
170
|
+
dataOwner: 'objectstorage:objectstorage',
|
|
171
|
+
recoverablePredecessors: [
|
|
172
|
+
{
|
|
173
|
+
entrypoint: '/bin/chown',
|
|
174
|
+
command: ['-R', 'objectstorage:objectstorage', '/data'],
|
|
175
|
+
},
|
|
176
|
+
],
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const noSqlDbProbeTimeoutCapMs = 5_000;
|
|
181
|
+
const noSqlDbProbeIntervalMs = 500;
|
|
182
|
+
|
|
183
|
+
/** The persistence-client surface used to prove NoSQLDB readiness. */
|
|
184
|
+
export interface INoSqlDbReadinessClient {
|
|
185
|
+
init(optionsArg: { timeoutMs: number }): Promise<void>;
|
|
186
|
+
checkReadiness(optionsArg: {
|
|
187
|
+
timeoutMs: number;
|
|
188
|
+
}): Promise<{ ready: true } | { ready: false; reason: string }>;
|
|
189
|
+
close(optionsArg: { timeoutMs: number }): Promise<void>;
|
|
190
|
+
}
|
|
115
191
|
|
|
116
192
|
export const objectStorageReconciliationTimeoutMs = 30_000;
|
|
117
193
|
const objectStorageProbeTimeoutCapMs = 2_000;
|
|
@@ -166,7 +242,7 @@ const normalizeObjectStorageLifecycleError = (errorArg: unknown): Error => {
|
|
|
166
242
|
: new Error('ObjectStorage lifecycle failed with a non-Error rejection.');
|
|
167
243
|
};
|
|
168
244
|
|
|
169
|
-
interface
|
|
245
|
+
interface IManagedContainerInspection {
|
|
170
246
|
id: string;
|
|
171
247
|
name: string;
|
|
172
248
|
running: boolean;
|
|
@@ -177,6 +253,7 @@ interface IObjectStorageContainerInspection {
|
|
|
177
253
|
labels: Record<string, string>;
|
|
178
254
|
mounts: Array<{ source: string; destination: string; readWrite: boolean }>;
|
|
179
255
|
restartPolicy: string;
|
|
256
|
+
networkMode: string;
|
|
180
257
|
command: string[];
|
|
181
258
|
entrypoint: string[];
|
|
182
259
|
user: string;
|
|
@@ -189,7 +266,7 @@ interface IObjectStorageContainerInspection {
|
|
|
189
266
|
};
|
|
190
267
|
}
|
|
191
268
|
|
|
192
|
-
interface
|
|
269
|
+
interface IManagedContainerIdentity {
|
|
193
270
|
id: string;
|
|
194
271
|
name: string;
|
|
195
272
|
running: boolean;
|
|
@@ -220,21 +297,22 @@ export const objectStorageExpectedHealthcheck = {
|
|
|
220
297
|
retries: 3,
|
|
221
298
|
};
|
|
222
299
|
|
|
223
|
-
const
|
|
300
|
+
const parseManagedContainerIdentity = (
|
|
224
301
|
stdoutArg: string,
|
|
225
302
|
expectedContainerNameArg: string,
|
|
226
|
-
|
|
303
|
+
displayNameArg: string,
|
|
304
|
+
): IManagedContainerIdentity => {
|
|
227
305
|
let parsed: unknown;
|
|
228
306
|
try {
|
|
229
307
|
parsed = JSON.parse(stdoutArg);
|
|
230
308
|
} catch {
|
|
231
309
|
throw new Error(
|
|
232
|
-
`Docker returned an invalid
|
|
310
|
+
`Docker returned an invalid ${displayNameArg} identity for ${expectedContainerNameArg}.`,
|
|
233
311
|
);
|
|
234
312
|
}
|
|
235
313
|
if (!Array.isArray(parsed) || parsed.length !== 1 || !isUnknownRecord(parsed[0])) {
|
|
236
314
|
throw new Error(
|
|
237
|
-
`Docker returned an invalid
|
|
315
|
+
`Docker returned an invalid ${displayNameArg} identity for ${expectedContainerNameArg}.`,
|
|
238
316
|
);
|
|
239
317
|
}
|
|
240
318
|
const inspected = parsed[0];
|
|
@@ -253,14 +331,14 @@ const parseObjectStorageIdentity = (
|
|
|
253
331
|
(config.Labels !== null && config.Labels !== undefined && !isUnknownRecord(config.Labels))
|
|
254
332
|
) {
|
|
255
333
|
throw new Error(
|
|
256
|
-
`Docker returned an invalid
|
|
334
|
+
`Docker returned an invalid ${displayNameArg} identity for ${expectedContainerNameArg}.`,
|
|
257
335
|
);
|
|
258
336
|
}
|
|
259
337
|
const labels: Record<string, string> = {};
|
|
260
338
|
for (const [keyArg, valueArg] of Object.entries(config.Labels || {})) {
|
|
261
339
|
if (typeof valueArg !== 'string') {
|
|
262
340
|
throw new Error(
|
|
263
|
-
`Docker returned an invalid
|
|
341
|
+
`Docker returned an invalid ${displayNameArg} identity for ${expectedContainerNameArg}.`,
|
|
264
342
|
);
|
|
265
343
|
}
|
|
266
344
|
labels[keyArg] = valueArg;
|
|
@@ -274,21 +352,22 @@ const parseObjectStorageIdentity = (
|
|
|
274
352
|
};
|
|
275
353
|
};
|
|
276
354
|
|
|
277
|
-
const
|
|
355
|
+
const parseManagedContainerInspection = (
|
|
278
356
|
stdoutArg: string,
|
|
279
357
|
expectedContainerNameArg: string,
|
|
280
|
-
|
|
358
|
+
displayNameArg: string,
|
|
359
|
+
): IManagedContainerInspection => {
|
|
281
360
|
let parsed: unknown;
|
|
282
361
|
try {
|
|
283
362
|
parsed = JSON.parse(stdoutArg);
|
|
284
363
|
} catch {
|
|
285
364
|
throw new Error(
|
|
286
|
-
`Docker returned an invalid
|
|
365
|
+
`Docker returned an invalid ${displayNameArg} inspection for ${expectedContainerNameArg}.`,
|
|
287
366
|
);
|
|
288
367
|
}
|
|
289
368
|
if (!Array.isArray(parsed) || parsed.length !== 1 || !isUnknownRecord(parsed[0])) {
|
|
290
369
|
throw new Error(
|
|
291
|
-
`Docker returned an invalid
|
|
370
|
+
`Docker returned an invalid ${displayNameArg} inspection for ${expectedContainerNameArg}.`,
|
|
292
371
|
);
|
|
293
372
|
}
|
|
294
373
|
|
|
@@ -318,6 +397,7 @@ const parseObjectStorageInspection = (
|
|
|
318
397
|
!isUnknownRecord(hostConfig.PortBindings) ||
|
|
319
398
|
!isUnknownRecord(restartPolicy) ||
|
|
320
399
|
typeof restartPolicy.Name !== 'string' ||
|
|
400
|
+
typeof hostConfig.NetworkMode !== 'string' ||
|
|
321
401
|
!Array.isArray(config.Env) ||
|
|
322
402
|
!config.Env.every((entryArg) => typeof entryArg === 'string') ||
|
|
323
403
|
!isUnknownRecord(config.Labels) ||
|
|
@@ -331,7 +411,7 @@ const parseObjectStorageInspection = (
|
|
|
331
411
|
!Array.isArray(inspected.Mounts)
|
|
332
412
|
) {
|
|
333
413
|
throw new Error(
|
|
334
|
-
`Docker returned an invalid
|
|
414
|
+
`Docker returned an invalid ${displayNameArg} inspection for ${expectedContainerNameArg}.`,
|
|
335
415
|
);
|
|
336
416
|
}
|
|
337
417
|
|
|
@@ -339,7 +419,7 @@ const parseObjectStorageInspection = (
|
|
|
339
419
|
for (const [key, value] of Object.entries(config.Labels)) {
|
|
340
420
|
if (typeof value !== 'string') {
|
|
341
421
|
throw new Error(
|
|
342
|
-
`Docker returned an invalid
|
|
422
|
+
`Docker returned an invalid ${displayNameArg} inspection for ${expectedContainerNameArg}.`,
|
|
343
423
|
);
|
|
344
424
|
}
|
|
345
425
|
labels[key] = value;
|
|
@@ -353,7 +433,7 @@ const parseObjectStorageInspection = (
|
|
|
353
433
|
typeof mountArg.RW !== 'boolean'
|
|
354
434
|
) {
|
|
355
435
|
throw new Error(
|
|
356
|
-
`Docker returned an invalid
|
|
436
|
+
`Docker returned an invalid ${displayNameArg} inspection for ${expectedContainerNameArg}.`,
|
|
357
437
|
);
|
|
358
438
|
}
|
|
359
439
|
return {
|
|
@@ -374,6 +454,7 @@ const parseObjectStorageInspection = (
|
|
|
374
454
|
labels,
|
|
375
455
|
mounts,
|
|
376
456
|
restartPolicy: restartPolicy.Name,
|
|
457
|
+
networkMode: hostConfig.NetworkMode,
|
|
377
458
|
command: [...config.Cmd],
|
|
378
459
|
entrypoint: [...config.Entrypoint],
|
|
379
460
|
user: config.User,
|
|
@@ -387,7 +468,7 @@ const parseObjectStorageInspection = (
|
|
|
387
468
|
};
|
|
388
469
|
};
|
|
389
470
|
|
|
390
|
-
const
|
|
471
|
+
const loopbackPortBindingMatches = (
|
|
391
472
|
portBindingsArg: Record<string, unknown>,
|
|
392
473
|
containerPortArg: string,
|
|
393
474
|
expectedHostPortArg: string,
|
|
@@ -402,6 +483,32 @@ const objectStoragePortMatches = (
|
|
|
402
483
|
);
|
|
403
484
|
};
|
|
404
485
|
|
|
486
|
+
const arraysEqual = (actualArg: readonly string[], expectedArg: readonly string[]): boolean =>
|
|
487
|
+
actualArg.length === expectedArg.length &&
|
|
488
|
+
actualArg.every((entryArg, indexArg) => entryArg === expectedArg[indexArg]);
|
|
489
|
+
|
|
490
|
+
const healthcheckMatches = (
|
|
491
|
+
actualArg: IManagedContainerInspection['healthcheck'],
|
|
492
|
+
expectedArg: typeof objectStorageExpectedHealthcheck,
|
|
493
|
+
): boolean =>
|
|
494
|
+
arraysEqual(actualArg.test, expectedArg.test) &&
|
|
495
|
+
actualArg.interval === expectedArg.interval &&
|
|
496
|
+
actualArg.timeout === expectedArg.timeout &&
|
|
497
|
+
actualArg.startPeriod === expectedArg.startPeriod &&
|
|
498
|
+
actualArg.retries === expectedArg.retries;
|
|
499
|
+
|
|
500
|
+
/** Replace every configured secret, longest first so overlaps cannot leak. */
|
|
501
|
+
const redactSecrets = (textArg: string, secretsArg: readonly string[]): string => {
|
|
502
|
+
const secrets = Array.from(new Set(secretsArg.filter(Boolean))).sort(
|
|
503
|
+
(secretA, secretB) => secretB.length - secretA.length,
|
|
504
|
+
);
|
|
505
|
+
let output = textArg;
|
|
506
|
+
for (const secret of secrets) {
|
|
507
|
+
output = output.split(secret).join('***');
|
|
508
|
+
}
|
|
509
|
+
return output;
|
|
510
|
+
};
|
|
511
|
+
|
|
405
512
|
const isS3BucketNotFound = (errorArg: unknown): boolean => {
|
|
406
513
|
if (!isUnknownRecord(errorArg)) {
|
|
407
514
|
return false;
|
|
@@ -540,6 +647,20 @@ export class ServiceManager {
|
|
|
540
647
|
private enabledServices: string[] | null = null;
|
|
541
648
|
private globalRegistry: GlobalRegistry;
|
|
542
649
|
private objectStorageClock: IObjectStorageReconciliationClock = defaultObjectStorageClock;
|
|
650
|
+
private noSqlDbClock: IObjectStorageReconciliationClock = defaultObjectStorageClock;
|
|
651
|
+
private noSqlDbFactory = (
|
|
652
|
+
configArg: IServiceConfig,
|
|
653
|
+
authEnabledArg: boolean,
|
|
654
|
+
): INoSqlDbReadinessClient =>
|
|
655
|
+
new plugins.nosqldb.SmartdataDb({
|
|
656
|
+
mongoDbUrl: authEnabledArg
|
|
657
|
+
? `mongodb://<USERNAME>:<PASSWORD>@127.0.0.1:${configArg.MONGODB_PORT}/?authSource=admin&directConnection=true`
|
|
658
|
+
: `mongodb://127.0.0.1:${configArg.MONGODB_PORT}/?directConnection=true`,
|
|
659
|
+
mongoDbName: configArg.MONGODB_NAME,
|
|
660
|
+
...(authEnabledArg
|
|
661
|
+
? { mongoDbUser: configArg.MONGODB_USER, mongoDbPass: configArg.MONGODB_PASS }
|
|
662
|
+
: {}),
|
|
663
|
+
});
|
|
543
664
|
private fetchImplementation: typeof fetch = globalThis.fetch;
|
|
544
665
|
private smartBucketFactory = (configArg: IServiceConfig) =>
|
|
545
666
|
new plugins.objectstorage.SmartBucket({
|
|
@@ -724,7 +845,7 @@ export class ServiceManager {
|
|
|
724
845
|
projectPath: process.cwd(),
|
|
725
846
|
projectName: config.PROJECT_NAME,
|
|
726
847
|
containers: {
|
|
727
|
-
|
|
848
|
+
nosqldb: containers.nosqldb,
|
|
728
849
|
objectstorage: containers.objectstorage,
|
|
729
850
|
elasticsearch: containers.elasticsearch,
|
|
730
851
|
},
|
|
@@ -777,12 +898,11 @@ export class ServiceManager {
|
|
|
777
898
|
}
|
|
778
899
|
|
|
779
900
|
/**
|
|
780
|
-
* Start MongoDB
|
|
901
|
+
* Start the `mongodb` service: a NoSQLDB engine speaking the MongoDB wire
|
|
902
|
+
* protocol, published on loopback only.
|
|
781
903
|
*
|
|
782
|
-
*
|
|
783
|
-
*
|
|
784
|
-
* MONGODB_PORT inside the container too, so the advertised member address
|
|
785
|
-
* (localhost:MONGODB_PORT) is reachable from host and container alike.
|
|
904
|
+
* Legacy mongod state is migrated first when its ownership is proven, and
|
|
905
|
+
* refused before any Docker mutation when it is not.
|
|
786
906
|
*/
|
|
787
907
|
public async startMongoDB(): Promise<void> {
|
|
788
908
|
await withServiceDataOperationLock(process.cwd(), 'mongodb', () =>
|
|
@@ -803,337 +923,537 @@ export class ServiceManager {
|
|
|
803
923
|
}
|
|
804
924
|
}
|
|
805
925
|
|
|
806
|
-
private
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
926
|
+
private createLegacyMongoMigration(): LegacyMongoMigration {
|
|
927
|
+
const getuid = process.getuid;
|
|
928
|
+
const getgid = process.getgid;
|
|
929
|
+
if (!getuid || !getgid) {
|
|
930
|
+
throw new Error('Migrating legacy MongoDB data requires a POSIX host user.');
|
|
931
|
+
}
|
|
932
|
+
return new LegacyMongoMigration({
|
|
933
|
+
projectPath: process.cwd(),
|
|
934
|
+
docker: this.docker,
|
|
935
|
+
config: this.config.getConfig(),
|
|
936
|
+
containerNames: this.config.getContainerNames(),
|
|
937
|
+
helperUser: `${getuid.call(process)}:${getgid.call(process)}`,
|
|
938
|
+
discoverContainerIdByExactName: (nameArg, timeoutArg) =>
|
|
939
|
+
this.discoverContainerIdByExactName(nameArg, timeoutArg),
|
|
940
|
+
getRegistryProjects: () => this.globalRegistry.getAllProjects(),
|
|
941
|
+
clearRegistryLegacyMongoContainer: (projectPathArg, nameArg) =>
|
|
942
|
+
this.globalRegistry.clearLegacyMongoContainer(projectPathArg, nameArg),
|
|
943
|
+
startMigrationNoSqlDb: () => this.startMigrationNoSqlDbContainer(),
|
|
944
|
+
stopMigrationNoSqlDb: () => this.stopMigrationNoSqlDbContainer(),
|
|
945
|
+
publishMigratedNoSqlDb: () => this.publishMigratedNoSqlDbContainer(),
|
|
946
|
+
log: (levelArg, messageArg) => logger.log(levelArg, messageArg),
|
|
947
|
+
});
|
|
948
|
+
}
|
|
811
949
|
|
|
812
|
-
|
|
950
|
+
private getNoSqlDbExpectedLabels(): Record<string, string> {
|
|
951
|
+
return getServiceContainerLabels({
|
|
952
|
+
projectPath: process.cwd(),
|
|
953
|
+
service: 'mongodb',
|
|
954
|
+
dataPath: this.config.getDataDirectories().nosqldb,
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* Create and start the restore-phase NoSQLDB container of a legacy mongod
|
|
960
|
+
* migration: no network, no published port, restart policy `no`. Only
|
|
961
|
+
* helpers that join its network namespace can reach it, so its data holds
|
|
962
|
+
* nothing but migration writes until verification commits. Refuses to adopt
|
|
963
|
+
* an existing container: the migration restores only into one it created.
|
|
964
|
+
*/
|
|
965
|
+
private async startMigrationNoSqlDbContainer(): Promise<{ id: string }> {
|
|
813
966
|
const containers = this.config.getContainerNames();
|
|
814
|
-
const
|
|
967
|
+
const timeout = () => ({ timeoutMs: noSqlDbReconciliationTimeoutMs });
|
|
968
|
+
if (await this.discoverContainerIdByExactName(containers.nosqldb, timeout)) {
|
|
969
|
+
throw new Error(
|
|
970
|
+
`NoSQLDB container ${containers.nosqldb} already exists; the migration restores only into a container it creates.`,
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
await this.prepareNoSqlDbDataDirectory(timeout);
|
|
974
|
+
const inspection = await this.createNoSqlDbContainer(
|
|
975
|
+
this.getNoSqlDbExpectedLabels(),
|
|
976
|
+
timeout,
|
|
977
|
+
'migration',
|
|
978
|
+
);
|
|
979
|
+
await this.docker.execArgv(['start', inspection.id], timeout());
|
|
980
|
+
return { id: inspection.id };
|
|
981
|
+
}
|
|
815
982
|
|
|
816
|
-
|
|
817
|
-
|
|
983
|
+
/** Inspect the canonical NoSQLDB container and prove it is the restore-phase shape. */
|
|
984
|
+
private async inspectMigrationNoSqlDbContainer(
|
|
985
|
+
timeoutArg: () => { timeoutMs: number },
|
|
986
|
+
): Promise<IManagedContainerInspection | undefined> {
|
|
987
|
+
const containers = this.config.getContainerNames();
|
|
988
|
+
const labels = this.getNoSqlDbExpectedLabels();
|
|
989
|
+
const id = await this.discoverContainerIdByExactName(containers.nosqldb, timeoutArg);
|
|
990
|
+
if (!id) {
|
|
991
|
+
return undefined;
|
|
992
|
+
}
|
|
993
|
+
const inspection = await this.revalidateNoSqlDbContainer(
|
|
994
|
+
id,
|
|
995
|
+
containers.nosqldb,
|
|
996
|
+
labels,
|
|
997
|
+
timeoutArg,
|
|
998
|
+
);
|
|
999
|
+
return this.noSqlDbConfigurationMatches(inspection, labels, 'migration') ? inspection : undefined;
|
|
1000
|
+
}
|
|
818
1001
|
|
|
819
|
-
|
|
1002
|
+
/** Gracefully stop the restore-phase container so its data is flushed. */
|
|
1003
|
+
private async stopMigrationNoSqlDbContainer(): Promise<void> {
|
|
1004
|
+
const containers = this.config.getContainerNames();
|
|
1005
|
+
const timeout = () => ({
|
|
1006
|
+
timeoutMs: (noSqlDbMigrationStopGraceSeconds + 60) * 1_000,
|
|
1007
|
+
});
|
|
1008
|
+
const inspection = await this.inspectMigrationNoSqlDbContainer(timeout);
|
|
1009
|
+
if (!inspection) {
|
|
1010
|
+
throw new Error(
|
|
1011
|
+
`The restore-phase NoSQLDB container ${containers.nosqldb} is missing or no longer has its unpublished shape.`,
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
if (inspection.running) {
|
|
1015
|
+
await this.docker.execArgv(
|
|
1016
|
+
['stop', '-t', String(noSqlDbMigrationStopGraceSeconds), inspection.id],
|
|
1017
|
+
timeout(),
|
|
1018
|
+
);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
820
1021
|
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
!inspectedContainer.HostConfig ||
|
|
837
|
-
!Object.hasOwn(inspectedContainer.HostConfig, 'PortBindings')
|
|
838
|
-
) {
|
|
839
|
-
throw new Error(
|
|
840
|
-
`Incomplete inspection for MongoDB container ${containers.mongo}; refusing to recreate it.`,
|
|
841
|
-
);
|
|
842
|
-
}
|
|
843
|
-
const containerCmd = inspectedContainer.Config.Cmd;
|
|
844
|
-
const hasReplSet = containerCmd.includes('--replSet');
|
|
845
|
-
const hasKeyFile = containerCmd.includes('--keyFile');
|
|
846
|
-
const mongoPortBindings =
|
|
847
|
-
inspectedContainer.HostConfig.PortBindings?.[`${config.MONGODB_PORT}/tcp`];
|
|
848
|
-
const portMatches =
|
|
849
|
-
Array.isArray(mongoPortBindings) &&
|
|
850
|
-
mongoPortBindings.length > 0 &&
|
|
851
|
-
mongoPortBindings.some(
|
|
852
|
-
(bindingArg: { HostPort?: string }) => bindingArg?.HostPort === config.MONGODB_PORT,
|
|
853
|
-
);
|
|
854
|
-
const authMatches = hasKeyFile === this.config.isMongoAuthEnabled();
|
|
855
|
-
const ulimitMatch = matchDockerUlimits(inspectInfo, mongoServiceUlimits);
|
|
856
|
-
if (ulimitMatch === 'unavailable') {
|
|
857
|
-
throw new Error(
|
|
858
|
-
`Unable to inspect MongoDB container limits for ${containers.mongo}; refusing to recreate it.`,
|
|
859
|
-
);
|
|
860
|
-
}
|
|
861
|
-
needsRecreate =
|
|
862
|
-
!hasReplSet ||
|
|
863
|
-
!portMatches ||
|
|
864
|
-
!authMatches ||
|
|
865
|
-
ulimitMatch === 'mismatch';
|
|
866
|
-
if (!authMatches) {
|
|
867
|
-
logger.log(
|
|
868
|
-
'note',
|
|
869
|
-
` Auth mode changed to ${this.config.isMongoAuthEnabled() ? 'enabled' : 'disabled'}, recreating container...`,
|
|
1022
|
+
/**
|
|
1023
|
+
* Replace the stopped restore-phase container with the published service
|
|
1024
|
+
* container on the same data directory and wait until it is ready.
|
|
1025
|
+
* Idempotent: once the service container exists it is only reconciled.
|
|
1026
|
+
*/
|
|
1027
|
+
private async publishMigratedNoSqlDbContainer(): Promise<{ id: string }> {
|
|
1028
|
+
const timeout = () => ({
|
|
1029
|
+
timeoutMs: (noSqlDbMigrationStopGraceSeconds + 60) * 1_000,
|
|
1030
|
+
});
|
|
1031
|
+
const restorePhase = await this.inspectMigrationNoSqlDbContainer(timeout);
|
|
1032
|
+
if (restorePhase) {
|
|
1033
|
+
if (restorePhase.running) {
|
|
1034
|
+
await this.docker.execArgv(
|
|
1035
|
+
['stop', '-t', String(noSqlDbMigrationStopGraceSeconds), restorePhase.id],
|
|
1036
|
+
timeout(),
|
|
870
1037
|
);
|
|
871
1038
|
}
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
1039
|
+
// Plain `rm` refuses a running container; the data is a bind mount and
|
|
1040
|
+
// the image declares no other volume.
|
|
1041
|
+
await this.docker.execArgv(['rm', restorePhase.id], timeout());
|
|
875
1042
|
}
|
|
1043
|
+
const { id } = await this.reconcileNoSqlDbContainer();
|
|
1044
|
+
return { id };
|
|
1045
|
+
}
|
|
876
1046
|
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
logger.log('note', ' MongoDB service configuration changed, recreating container...');
|
|
884
|
-
await this.docker.remove(containers.mongo, true);
|
|
885
|
-
await this.createMongoContainer();
|
|
886
|
-
break;
|
|
887
|
-
|
|
888
|
-
case 'stopped':
|
|
889
|
-
if (needsRecreate) {
|
|
890
|
-
logger.log('note', ' Configuration changed, recreating container...');
|
|
891
|
-
await this.docker.remove(containers.mongo, true);
|
|
892
|
-
await this.createMongoContainer();
|
|
893
|
-
} else {
|
|
894
|
-
if (await this.docker.start(containers.mongo)) {
|
|
895
|
-
logger.log('ok', ' Started ✓');
|
|
896
|
-
} else {
|
|
897
|
-
logger.log('error', ' Failed to start');
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
break;
|
|
901
|
-
|
|
902
|
-
case 'not_exists':
|
|
903
|
-
logger.log('note', ' Creating container...');
|
|
904
|
-
await this.createMongoContainer();
|
|
905
|
-
break;
|
|
906
|
-
}
|
|
1047
|
+
private async startMongoDBLocked(): Promise<void> {
|
|
1048
|
+
await this.config.reloadFromDisk();
|
|
1049
|
+
await this.assertNoPendingDataDeletion('mongodb');
|
|
1050
|
+
await this.globalRegistry.touchProject(process.cwd());
|
|
1051
|
+
logger.log('note', '📦 MongoDB (NoSQLDB):');
|
|
1052
|
+
this.assertNoSqlDbConfigurationSafe();
|
|
907
1053
|
|
|
908
|
-
await this.
|
|
909
|
-
await this.
|
|
1054
|
+
await this.createLegacyMongoMigration().run();
|
|
1055
|
+
const { outcome } = await this.reconcileNoSqlDbContainer();
|
|
910
1056
|
|
|
911
|
-
|
|
912
|
-
|
|
1057
|
+
const config = this.config.getConfig();
|
|
1058
|
+
const containers = this.config.getContainerNames();
|
|
1059
|
+
logger.log('ok', ` ${outcome} ✓`);
|
|
1060
|
+
logger.log('info', ` Container: ${containers.nosqldb}`);
|
|
1061
|
+
logger.log('info', ` Port: 127.0.0.1:${config.MONGODB_PORT}`);
|
|
913
1062
|
if (!this.config.isMongoAuthEnabled()) {
|
|
914
|
-
logger.log(
|
|
915
|
-
'note',
|
|
916
|
-
` ⚠️ Auth: DISABLED — published on loopback only (127.0.0.1), ${this.describeMongoAuthSource()}`,
|
|
917
|
-
);
|
|
1063
|
+
logger.log('note', ` ⚠️ Auth: DISABLED, ${this.describeMongoAuthSource()}`);
|
|
918
1064
|
}
|
|
919
1065
|
logger.log('info', ` Connection: ${this.config.getMongoConnectionString()}`);
|
|
1066
|
+
}
|
|
920
1067
|
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
1068
|
+
/** Read-only preflight used before restart or reconfigure may stop anything. */
|
|
1069
|
+
public async assertMongoStartupAllowed(): Promise<void> {
|
|
1070
|
+
this.assertNoSqlDbConfigurationSafe();
|
|
1071
|
+
const assessment = await this.createLegacyMongoMigration().assessPending();
|
|
1072
|
+
if (assessment.kind === 'blocked') {
|
|
1073
|
+
throw new Error(
|
|
1074
|
+
`Legacy MongoDB state blocks NoSQLDB startup: ${assessment.evidence.join(', ')}. ${assessment.reason} ` +
|
|
1075
|
+
'Run `gitzone services start mongo` for exact recovery instructions.',
|
|
1076
|
+
);
|
|
927
1077
|
}
|
|
928
1078
|
}
|
|
929
1079
|
|
|
1080
|
+
private assertNoSqlDbConfigurationSafe(): void {
|
|
1081
|
+
this.config.assertMongoExposureIsSafe();
|
|
1082
|
+
this.config.assertNoSqlDbCredentialsAreSafe();
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
private getNoSqlDbEnvironment(): Record<string, string> {
|
|
1086
|
+
const config = this.config.getConfig();
|
|
1087
|
+
const authEnabled = this.config.isMongoAuthEnabled();
|
|
1088
|
+
return {
|
|
1089
|
+
DBST_PORT: noSqlDbWirePort,
|
|
1090
|
+
DBST_UI_PORT: noSqlDbUiPort,
|
|
1091
|
+
DBST_STORAGE_DIR: '/data',
|
|
1092
|
+
DBST_AUTH_ENABLED: authEnabled ? 'true' : 'false',
|
|
1093
|
+
DBST_ADMIN_PASSWORD: config.MONGODB_ADMIN_PASSWORD,
|
|
1094
|
+
...(authEnabled
|
|
1095
|
+
? { DBST_ROOT_USERNAME: config.MONGODB_USER, DBST_ROOT_PASSWORD: config.MONGODB_PASS }
|
|
1096
|
+
: {}),
|
|
1097
|
+
};
|
|
1098
|
+
}
|
|
1099
|
+
|
|
930
1100
|
/**
|
|
931
|
-
*
|
|
1101
|
+
* Whether a NoSQLDB container has exactly the runtime configuration of one
|
|
1102
|
+
* lifecycle shape. `service` is the published, auto-restarting service;
|
|
1103
|
+
* `migration` is the restore-phase container of a legacy mongod migration:
|
|
1104
|
+
* no network, no published port, restart policy `no`, so no application can
|
|
1105
|
+
* reach or revive it before its data is verified.
|
|
932
1106
|
*/
|
|
933
|
-
private
|
|
1107
|
+
private noSqlDbConfigurationMatches(
|
|
1108
|
+
inspectionArg: IManagedContainerInspection,
|
|
1109
|
+
expectedLabelsArg: Record<string, string>,
|
|
1110
|
+
shapeArg: TNoSqlDbContainerShape = 'service',
|
|
1111
|
+
): boolean {
|
|
934
1112
|
const config = this.config.getConfig();
|
|
935
|
-
const containers = this.config.getContainerNames();
|
|
936
1113
|
const directories = this.config.getDataDirectories();
|
|
937
|
-
const
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
1114
|
+
const expectedEnvironment = this.getNoSqlDbEnvironment();
|
|
1115
|
+
const controlledEnvironmentEntries = inspectionArg.environmentEntries.filter((entryArg) =>
|
|
1116
|
+
entryArg.startsWith('DBST_'),
|
|
1117
|
+
);
|
|
1118
|
+
const readEnvironment = (nameArg: string): string | undefined => {
|
|
1119
|
+
const prefix = `${nameArg}=`;
|
|
1120
|
+
const matches = inspectionArg.environmentEntries.filter((entry) =>
|
|
1121
|
+
entry.startsWith(prefix),
|
|
1122
|
+
);
|
|
1123
|
+
return matches.length === 1 ? matches[0].slice(prefix.length) : undefined;
|
|
1124
|
+
};
|
|
1125
|
+
const portKeys = Object.keys(inspectionArg.portBindings).sort();
|
|
1126
|
+
const exposureMatches =
|
|
1127
|
+
shapeArg === 'service'
|
|
1128
|
+
? portKeys.length === 1 &&
|
|
1129
|
+
portKeys[0] === `${noSqlDbWirePort}/tcp` &&
|
|
1130
|
+
loopbackPortBindingMatches(
|
|
1131
|
+
inspectionArg.portBindings,
|
|
1132
|
+
`${noSqlDbWirePort}/tcp`,
|
|
1133
|
+
config.MONGODB_PORT,
|
|
1134
|
+
) &&
|
|
1135
|
+
inspectionArg.networkMode !== 'none' &&
|
|
1136
|
+
inspectionArg.restartPolicy === 'unless-stopped'
|
|
1137
|
+
: portKeys.length === 0 &&
|
|
1138
|
+
inspectionArg.networkMode === 'none' &&
|
|
1139
|
+
inspectionArg.restartPolicy === 'no';
|
|
1140
|
+
return (
|
|
1141
|
+
inspectionArg.image === noSqlDbImage &&
|
|
1142
|
+
exposureMatches &&
|
|
1143
|
+
inspectionArg.mounts.length === 1 &&
|
|
1144
|
+
inspectionArg.mounts[0].source === plugins.path.resolve(directories.nosqldb) &&
|
|
1145
|
+
inspectionArg.mounts[0].destination === '/data' &&
|
|
1146
|
+
inspectionArg.mounts[0].readWrite &&
|
|
1147
|
+
controlledEnvironmentEntries.length === Object.keys(expectedEnvironment).length &&
|
|
1148
|
+
Object.entries(expectedEnvironment).every(
|
|
1149
|
+
([name, value]) => readEnvironment(name) === value,
|
|
1150
|
+
) &&
|
|
1151
|
+
hasExactServiceContainerLabels(inspectionArg.labels, expectedLabelsArg) &&
|
|
1152
|
+
arraysEqual(inspectionArg.command, noSqlDbExpectedCommand) &&
|
|
1153
|
+
arraysEqual(inspectionArg.entrypoint, noSqlDbExpectedEntrypoint) &&
|
|
1154
|
+
inspectionArg.user === noSqlDbContainerUser &&
|
|
1155
|
+
healthcheckMatches(inspectionArg.healthcheck, noSqlDbExpectedHealthcheck)
|
|
1156
|
+
);
|
|
1157
|
+
}
|
|
941
1158
|
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
1159
|
+
private async inspectNoSqlDbContainer(
|
|
1160
|
+
containerReferenceArg: string,
|
|
1161
|
+
expectedContainerNameArg: string,
|
|
1162
|
+
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1163
|
+
): Promise<IManagedContainerInspection> {
|
|
1164
|
+
const result = await this.docker.execArgv(
|
|
1165
|
+
['inspect', containerReferenceArg],
|
|
1166
|
+
dockerTimeoutArg(),
|
|
1167
|
+
);
|
|
1168
|
+
return parseManagedContainerInspection(result.stdout, expectedContainerNameArg, 'NoSQLDB');
|
|
1169
|
+
}
|
|
947
1170
|
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
const
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
1171
|
+
private async revalidateNoSqlDbContainer(
|
|
1172
|
+
containerIdArg: string,
|
|
1173
|
+
expectedContainerNameArg: string,
|
|
1174
|
+
expectedLabelsArg: Record<string, string>,
|
|
1175
|
+
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1176
|
+
): Promise<IManagedContainerInspection> {
|
|
1177
|
+
const inspection = await this.inspectNoSqlDbContainer(
|
|
1178
|
+
containerIdArg,
|
|
1179
|
+
expectedContainerNameArg,
|
|
1180
|
+
dockerTimeoutArg,
|
|
1181
|
+
);
|
|
1182
|
+
if (inspection.id !== containerIdArg) {
|
|
1183
|
+
throw new Error(
|
|
1184
|
+
`Refusing to use NoSQLDB container ${expectedContainerNameArg}: ` +
|
|
1185
|
+
'its immutable id changed during reconciliation.',
|
|
1186
|
+
);
|
|
960
1187
|
}
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
await this.ensureMongoKeyfile(directories.mongo);
|
|
1188
|
+
if (!hasExactServiceContainerLabels(inspection.labels, expectedLabelsArg)) {
|
|
1189
|
+
throw new Error(
|
|
1190
|
+
`Refusing to use NoSQLDB container ${inspection.name}: ` +
|
|
1191
|
+
'its ownership is not proven for the current project.',
|
|
1192
|
+
);
|
|
967
1193
|
}
|
|
1194
|
+
return inspection;
|
|
1195
|
+
}
|
|
968
1196
|
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
},
|
|
975
|
-
volumes: {
|
|
976
|
-
[directories.mongo]: '/data/db'
|
|
977
|
-
},
|
|
978
|
-
environment,
|
|
979
|
-
labels,
|
|
980
|
-
ulimits: mongoServiceUlimits,
|
|
981
|
-
restart: 'unless-stopped',
|
|
982
|
-
command
|
|
983
|
-
});
|
|
1197
|
+
private async prepareNoSqlDbDataDirectory(
|
|
1198
|
+
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1199
|
+
): Promise<void> {
|
|
1200
|
+
await this.prepareHardenedDataDirectory('mongodb', dockerTimeoutArg);
|
|
1201
|
+
}
|
|
984
1202
|
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1203
|
+
private async createNoSqlDbContainer(
|
|
1204
|
+
labelsArg: Record<string, string>,
|
|
1205
|
+
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1206
|
+
shapeArg: TNoSqlDbContainerShape = 'service',
|
|
1207
|
+
): Promise<IManagedContainerInspection> {
|
|
1208
|
+
const config = this.config.getConfig();
|
|
1209
|
+
const containers = this.config.getContainerNames();
|
|
1210
|
+
const directories = this.config.getDataDirectories();
|
|
1211
|
+
const creationResult = await this.docker.createArgv(
|
|
1212
|
+
{
|
|
1213
|
+
name: containers.nosqldb,
|
|
1214
|
+
image: noSqlDbImage,
|
|
1215
|
+
...(shapeArg === 'service'
|
|
1216
|
+
? {
|
|
1217
|
+
ports: { [`127.0.0.1:${config.MONGODB_PORT}`]: noSqlDbWirePort },
|
|
1218
|
+
restart: 'unless-stopped',
|
|
1219
|
+
}
|
|
1220
|
+
: { network: 'none', restart: 'no' }),
|
|
1221
|
+
volumes: { [directories.nosqldb]: '/data' },
|
|
1222
|
+
environment: this.getNoSqlDbEnvironment(),
|
|
1223
|
+
labels: labelsArg,
|
|
1224
|
+
},
|
|
1225
|
+
dockerTimeoutArg(),
|
|
1226
|
+
);
|
|
1227
|
+
const containerId = creationResult.stdout.trim();
|
|
1228
|
+
if (!/^[a-f0-9]{64}$/u.test(containerId)) {
|
|
1229
|
+
throw new Error(`Docker returned an invalid id while creating ${containers.nosqldb}.`);
|
|
1230
|
+
}
|
|
1231
|
+
const inspection = await this.revalidateNoSqlDbContainer(
|
|
1232
|
+
containerId,
|
|
1233
|
+
containers.nosqldb,
|
|
1234
|
+
labelsArg,
|
|
1235
|
+
dockerTimeoutArg,
|
|
1236
|
+
);
|
|
1237
|
+
if (inspection.running || inspection.status !== 'created') {
|
|
1238
|
+
throw new Error(`NoSQLDB container ${containers.nosqldb} was not created stopped.`);
|
|
1239
|
+
}
|
|
1240
|
+
if (!this.noSqlDbConfigurationMatches(inspection, labelsArg, shapeArg)) {
|
|
1241
|
+
throw new Error(
|
|
1242
|
+
`NoSQLDB container ${containers.nosqldb} does not match the required runtime configuration.`,
|
|
1243
|
+
);
|
|
990
1244
|
}
|
|
1245
|
+
return inspection;
|
|
991
1246
|
}
|
|
992
1247
|
|
|
993
|
-
/**
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1248
|
+
/** Authenticated wire-protocol readiness through the persistence client. */
|
|
1249
|
+
private async waitForNoSqlDbReadiness(deadlineAtArg: number): Promise<void> {
|
|
1250
|
+
const config = this.config.getConfig();
|
|
1251
|
+
let lastReason = 'not attempted';
|
|
1252
|
+
while (true) {
|
|
1253
|
+
const remaining = Math.floor(deadlineAtArg - this.noSqlDbClock.now());
|
|
1254
|
+
if (remaining <= 0) {
|
|
1255
|
+
throw new Error(`NoSQLDB readiness timed out (last result: ${lastReason}).`);
|
|
1256
|
+
}
|
|
1257
|
+
const probeTimeoutMs = Math.max(1, Math.min(noSqlDbProbeTimeoutCapMs, remaining));
|
|
1258
|
+
const database = this.noSqlDbFactory(config, this.config.isMongoAuthEnabled());
|
|
1259
|
+
try {
|
|
1260
|
+
await database.init({ timeoutMs: probeTimeoutMs });
|
|
1261
|
+
const readiness = await database.checkReadiness({ timeoutMs: probeTimeoutMs });
|
|
1262
|
+
if (readiness.ready) {
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
lastReason = readiness.reason;
|
|
1266
|
+
} catch {
|
|
1267
|
+
lastReason = 'connection failed';
|
|
1268
|
+
} finally {
|
|
1269
|
+
await database.close({ timeoutMs: noSqlDbProbeTimeoutCapMs }).catch(() => {});
|
|
1270
|
+
}
|
|
1271
|
+
const afterProbe = Math.floor(deadlineAtArg - this.noSqlDbClock.now());
|
|
1272
|
+
if (afterProbe <= 0) {
|
|
1273
|
+
throw new Error(`NoSQLDB readiness timed out (last result: ${lastReason}).`);
|
|
1274
|
+
}
|
|
1275
|
+
await this.noSqlDbClock.delay(Math.min(noSqlDbProbeIntervalMs, afterProbe));
|
|
1002
1276
|
}
|
|
1003
|
-
logger.log('note', ' Generating replica set keyfile...');
|
|
1004
|
-
await this.docker.runOneOff({
|
|
1005
|
-
image: 'mongo:7.0',
|
|
1006
|
-
command:
|
|
1007
|
-
'openssl rand -base64 756 > /data/db/mongo-keyfile && chown 999:999 /data/db/mongo-keyfile && chmod 400 /data/db/mongo-keyfile',
|
|
1008
|
-
volumes: { [mongoDataDir]: '/data/db' },
|
|
1009
|
-
user: '0'
|
|
1010
|
-
});
|
|
1011
1277
|
}
|
|
1012
1278
|
|
|
1013
1279
|
/**
|
|
1014
|
-
*
|
|
1015
|
-
*
|
|
1016
|
-
*
|
|
1017
|
-
* that was first created with auth disabled has no user at all. Enabling auth
|
|
1018
|
-
* over it would leave an unusable database. MongoDB's localhost exception is
|
|
1019
|
-
* the designed bootstrap path for exactly this: while zero users exist, a
|
|
1020
|
-
* connection from localhost — which, inside the container, is what this is —
|
|
1021
|
-
* may create the first one. If users already exist the exception does not
|
|
1022
|
-
* apply and creation fails, so this can never overwrite or escalate anything.
|
|
1280
|
+
* Create, start, or reconcile the canonical NoSQLDB container by immutable id
|
|
1281
|
+
* and wait until it answers authenticated requests. Containers started by
|
|
1282
|
+
* this invocation are stopped again when reconciliation fails.
|
|
1023
1283
|
*/
|
|
1024
|
-
private async
|
|
1025
|
-
if (!this.config.isMongoAuthEnabled()) {
|
|
1026
|
-
return;
|
|
1027
|
-
}
|
|
1028
|
-
const config = this.config.getConfig();
|
|
1284
|
+
private async reconcileNoSqlDbContainer(): Promise<{ id: string; outcome: string }> {
|
|
1029
1285
|
const containers = this.config.getContainerNames();
|
|
1286
|
+
const directories = this.config.getDataDirectories();
|
|
1287
|
+
const deadlineAt = this.noSqlDbClock.now() + noSqlDbReconciliationTimeoutMs;
|
|
1288
|
+
const dockerTimeout = (): { timeoutMs: number } => {
|
|
1289
|
+
const remaining = Math.floor(deadlineAt - this.noSqlDbClock.now());
|
|
1290
|
+
if (remaining <= 0) {
|
|
1291
|
+
throw new Error('NoSQLDB container setup timed out.');
|
|
1292
|
+
}
|
|
1293
|
+
return { timeoutMs: Math.max(1, remaining) };
|
|
1294
|
+
};
|
|
1295
|
+
const expectedLabels = getServiceContainerLabels({
|
|
1296
|
+
projectPath: process.cwd(),
|
|
1297
|
+
service: 'mongodb',
|
|
1298
|
+
dataPath: directories.nosqldb,
|
|
1299
|
+
});
|
|
1300
|
+
const startedContainerIds: string[] = [];
|
|
1301
|
+
const startById = async (containerIdArg: string, ownedByInvocationArg: boolean) => {
|
|
1302
|
+
const executionOptions = dockerTimeout();
|
|
1303
|
+
if (ownedByInvocationArg) {
|
|
1304
|
+
startedContainerIds.push(containerIdArg);
|
|
1305
|
+
}
|
|
1306
|
+
await this.docker.execArgv(['start', containerIdArg], executionOptions);
|
|
1307
|
+
if (!ownedByInvocationArg) {
|
|
1308
|
+
startedContainerIds.push(containerIdArg);
|
|
1309
|
+
}
|
|
1310
|
+
};
|
|
1030
1311
|
|
|
1031
|
-
|
|
1032
|
-
const
|
|
1033
|
-
containers.
|
|
1034
|
-
|
|
1035
|
-
`-u "${config.MONGODB_USER}" -p "${config.MONGODB_PASS}" --authenticationDatabase admin ` +
|
|
1036
|
-
`--eval 'print("AUTH_OK")'`,
|
|
1312
|
+
try {
|
|
1313
|
+
const existingId = await this.discoverContainerIdByExactName(
|
|
1314
|
+
containers.nosqldb,
|
|
1315
|
+
dockerTimeout,
|
|
1037
1316
|
);
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1317
|
+
let initialInspection: IManagedContainerInspection | undefined;
|
|
1318
|
+
if (existingId) {
|
|
1319
|
+
initialInspection = await this.revalidateNoSqlDbContainer(
|
|
1320
|
+
existingId,
|
|
1321
|
+
containers.nosqldb,
|
|
1322
|
+
expectedLabels,
|
|
1323
|
+
dockerTimeout,
|
|
1324
|
+
);
|
|
1325
|
+
await this.repairServiceMarkerFromOwnedContainer('mongodb', directories.nosqldb);
|
|
1326
|
+
} else {
|
|
1327
|
+
await this.config.validateNoSqlDbPort();
|
|
1043
1328
|
}
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1329
|
+
|
|
1330
|
+
if (initialInspection?.status === 'restarting') {
|
|
1331
|
+
logger.log('note', ' Stopping restart loop before securing data...');
|
|
1332
|
+
await this.docker.execArgv(['stop', initialInspection.id], dockerTimeout());
|
|
1333
|
+
initialInspection = await this.revalidateNoSqlDbContainer(
|
|
1334
|
+
initialInspection.id,
|
|
1335
|
+
containers.nosqldb,
|
|
1336
|
+
expectedLabels,
|
|
1337
|
+
dockerTimeout,
|
|
1050
1338
|
);
|
|
1339
|
+
if (initialInspection.running || initialInspection.status === 'restarting') {
|
|
1340
|
+
throw new Error(`NoSQLDB container ${containers.nosqldb} remained in its restart loop.`);
|
|
1341
|
+
}
|
|
1051
1342
|
}
|
|
1052
|
-
// mongod not accepting connections yet
|
|
1053
|
-
await plugins.smartdelay.delayFor(1000);
|
|
1054
|
-
}
|
|
1055
|
-
|
|
1056
|
-
logger.log('note', ' No usable root user in existing data, bootstrapping...');
|
|
1057
|
-
const createScript =
|
|
1058
|
-
`db.getSiblingDB("admin").createUser({user: "${config.MONGODB_USER}", ` +
|
|
1059
|
-
`pwd: "${config.MONGODB_PASS}", roles: [{role: "root", db: "admin"}]}); ` +
|
|
1060
|
-
'print("USER_CREATED");';
|
|
1061
|
-
const create = await this.docker.execCombined(
|
|
1062
|
-
containers.mongo,
|
|
1063
|
-
`mongosh --quiet --port ${config.MONGODB_PORT} --eval '${createScript}'`,
|
|
1064
|
-
);
|
|
1065
|
-
if (create.output.includes('USER_CREATED')) {
|
|
1066
|
-
logger.log('ok', ' Root user created ✓');
|
|
1067
|
-
return;
|
|
1068
|
-
}
|
|
1069
1343
|
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1344
|
+
const needsRecreate =
|
|
1345
|
+
!!initialInspection &&
|
|
1346
|
+
!this.noSqlDbConfigurationMatches(initialInspection, expectedLabels);
|
|
1347
|
+
let activeInspection: IManagedContainerInspection;
|
|
1348
|
+
let outcome: string;
|
|
1349
|
+
if (!initialInspection || needsRecreate) {
|
|
1350
|
+
await this.prepareNoSqlDbDataDirectory(dockerTimeout);
|
|
1351
|
+
if (initialInspection) {
|
|
1352
|
+
logger.log('note', ' Runtime configuration changed, recreating container...');
|
|
1353
|
+
await this.revalidateNoSqlDbContainer(
|
|
1354
|
+
initialInspection.id,
|
|
1355
|
+
containers.nosqldb,
|
|
1356
|
+
expectedLabels,
|
|
1357
|
+
dockerTimeout,
|
|
1358
|
+
);
|
|
1359
|
+
await this.docker.execArgv(['rm', '-f', initialInspection.id], dockerTimeout());
|
|
1360
|
+
} else {
|
|
1361
|
+
logger.log('note', ' Creating container...');
|
|
1362
|
+
}
|
|
1363
|
+
activeInspection = await this.createNoSqlDbContainer(expectedLabels, dockerTimeout);
|
|
1364
|
+
await startById(activeInspection.id, true);
|
|
1365
|
+
outcome = initialInspection
|
|
1366
|
+
? 'Recreated with canonical configuration and ready'
|
|
1367
|
+
: 'Created, started, and ready';
|
|
1368
|
+
} else if (!initialInspection.running) {
|
|
1369
|
+
await this.prepareNoSqlDbDataDirectory(dockerTimeout);
|
|
1370
|
+
logger.log('note', ' Starting existing container...');
|
|
1371
|
+
activeInspection = initialInspection;
|
|
1372
|
+
await startById(activeInspection.id, false);
|
|
1373
|
+
outcome = 'Started and ready';
|
|
1374
|
+
} else {
|
|
1375
|
+
activeInspection = initialInspection;
|
|
1376
|
+
outcome = 'Already running and ready';
|
|
1377
|
+
}
|
|
1077
1378
|
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1379
|
+
await this.waitForNoSqlDbReadiness(deadlineAt);
|
|
1380
|
+
const finalInspection = await this.revalidateNoSqlDbContainer(
|
|
1381
|
+
activeInspection.id,
|
|
1382
|
+
containers.nosqldb,
|
|
1383
|
+
expectedLabels,
|
|
1384
|
+
dockerTimeout,
|
|
1385
|
+
);
|
|
1386
|
+
if (!finalInspection.running) {
|
|
1387
|
+
throw new Error(`NoSQLDB container ${containers.nosqldb} stopped during reconciliation.`);
|
|
1388
|
+
}
|
|
1389
|
+
return { id: activeInspection.id, outcome };
|
|
1390
|
+
} catch (error) {
|
|
1391
|
+
const originalError =
|
|
1392
|
+
error instanceof Error ? error : new Error('NoSQLDB lifecycle failed with a non-Error rejection.');
|
|
1393
|
+
const rollbackErrors: Error[] = [];
|
|
1394
|
+
for (let index = startedContainerIds.length - 1; index >= 0; index--) {
|
|
1395
|
+
try {
|
|
1396
|
+
const rollbackInspection = await this.revalidateNoSqlDbContainer(
|
|
1397
|
+
startedContainerIds[index],
|
|
1398
|
+
containers.nosqldb,
|
|
1399
|
+
expectedLabels,
|
|
1400
|
+
() => ({ timeoutMs: objectStorageRollbackTimeoutMs }),
|
|
1401
|
+
);
|
|
1402
|
+
if (rollbackInspection.running) {
|
|
1403
|
+
await this.docker.execArgv(['stop', startedContainerIds[index]], {
|
|
1404
|
+
timeoutMs: objectStorageRollbackTimeoutMs,
|
|
1405
|
+
});
|
|
1406
|
+
}
|
|
1407
|
+
} catch (rollbackError) {
|
|
1408
|
+
rollbackErrors.push(
|
|
1409
|
+
rollbackError instanceof Error ? rollbackError : new Error('NoSQLDB rollback failed.'),
|
|
1410
|
+
);
|
|
1411
|
+
}
|
|
1102
1412
|
}
|
|
1103
|
-
if (
|
|
1104
|
-
|
|
1413
|
+
if (rollbackErrors.length > 0) {
|
|
1414
|
+
throw new AggregateError(
|
|
1415
|
+
[originalError, ...rollbackErrors],
|
|
1416
|
+
'NoSQLDB startup failed and rollback could not stop every container started by this invocation.',
|
|
1417
|
+
);
|
|
1105
1418
|
}
|
|
1106
|
-
|
|
1419
|
+
throw originalError;
|
|
1107
1420
|
}
|
|
1108
|
-
logger.log('error', ' Replica set not ready after 30s — transactions unavailable');
|
|
1109
|
-
return false;
|
|
1110
1421
|
}
|
|
1111
1422
|
|
|
1112
1423
|
private async inspectObjectStorageContainer(
|
|
1113
1424
|
containerReferenceArg: string,
|
|
1114
1425
|
expectedContainerNameArg: string,
|
|
1115
1426
|
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1116
|
-
): Promise<
|
|
1427
|
+
): Promise<IManagedContainerInspection> {
|
|
1117
1428
|
const result = await this.docker.execArgv(
|
|
1118
1429
|
['inspect', containerReferenceArg],
|
|
1119
1430
|
dockerTimeoutArg(),
|
|
1120
1431
|
);
|
|
1121
|
-
return
|
|
1432
|
+
return parseManagedContainerInspection(
|
|
1433
|
+
result.stdout,
|
|
1434
|
+
expectedContainerNameArg,
|
|
1435
|
+
'ObjectStorage',
|
|
1436
|
+
);
|
|
1122
1437
|
}
|
|
1123
1438
|
|
|
1124
|
-
private async
|
|
1439
|
+
private async inspectManagedContainerIdentity(
|
|
1125
1440
|
containerReferenceArg: string,
|
|
1126
1441
|
expectedContainerNameArg: string,
|
|
1442
|
+
displayNameArg: string,
|
|
1127
1443
|
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1128
|
-
): Promise<
|
|
1444
|
+
): Promise<IManagedContainerIdentity> {
|
|
1129
1445
|
const result = await this.docker.execArgv(
|
|
1130
1446
|
['inspect', containerReferenceArg],
|
|
1131
1447
|
dockerTimeoutArg(),
|
|
1132
1448
|
);
|
|
1133
|
-
const identity =
|
|
1449
|
+
const identity = parseManagedContainerIdentity(
|
|
1450
|
+
result.stdout,
|
|
1451
|
+
expectedContainerNameArg,
|
|
1452
|
+
displayNameArg,
|
|
1453
|
+
);
|
|
1134
1454
|
if (/^[a-f0-9]{64}$/u.test(containerReferenceArg) && identity.id !== containerReferenceArg) {
|
|
1135
1455
|
throw new Error(
|
|
1136
|
-
`Refusing
|
|
1456
|
+
`Refusing ${displayNameArg} container ${expectedContainerNameArg}: its immutable id changed.`,
|
|
1137
1457
|
);
|
|
1138
1458
|
}
|
|
1139
1459
|
return identity;
|
|
@@ -1170,7 +1490,7 @@ export class ServiceManager {
|
|
|
1170
1490
|
}
|
|
1171
1491
|
|
|
1172
1492
|
private assertObjectStorageOwnership(
|
|
1173
|
-
inspectionArg:
|
|
1493
|
+
inspectionArg: IManagedContainerInspection,
|
|
1174
1494
|
expectedLabelsArg: Record<string, string>,
|
|
1175
1495
|
): void {
|
|
1176
1496
|
if (!hasExactServiceContainerLabels(inspectionArg.labels, expectedLabelsArg)) {
|
|
@@ -1186,7 +1506,7 @@ export class ServiceManager {
|
|
|
1186
1506
|
expectedContainerNameArg: string,
|
|
1187
1507
|
expectedLabelsArg: Record<string, string>,
|
|
1188
1508
|
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1189
|
-
): Promise<
|
|
1509
|
+
): Promise<IManagedContainerInspection> {
|
|
1190
1510
|
const inspection = await this.inspectObjectStorageContainer(
|
|
1191
1511
|
containerIdArg,
|
|
1192
1512
|
expectedContainerNameArg,
|
|
@@ -1203,7 +1523,7 @@ export class ServiceManager {
|
|
|
1203
1523
|
}
|
|
1204
1524
|
|
|
1205
1525
|
private objectStorageConfigurationMatches(
|
|
1206
|
-
inspectionArg:
|
|
1526
|
+
inspectionArg: IManagedContainerInspection,
|
|
1207
1527
|
expectedLabelsArg: Record<string, string>,
|
|
1208
1528
|
): boolean {
|
|
1209
1529
|
const config = this.config.getConfig();
|
|
@@ -1244,8 +1564,8 @@ export class ServiceManager {
|
|
|
1244
1564
|
inspectionArg.image === objectStorageImage &&
|
|
1245
1565
|
portKeys.length === expectedPortKeys.length &&
|
|
1246
1566
|
portKeys.every((entry, index) => entry === expectedPortKeys[index]) &&
|
|
1247
|
-
|
|
1248
|
-
|
|
1567
|
+
loopbackPortBindingMatches(inspectionArg.portBindings, '9000/tcp', config.S3_PORT) &&
|
|
1568
|
+
loopbackPortBindingMatches(inspectionArg.portBindings, '3000/tcp', config.S3_UI_PORT) &&
|
|
1249
1569
|
inspectionArg.mounts.length === 1 &&
|
|
1250
1570
|
inspectionArg.mounts[0].source === plugins.path.resolve(directories.objectstorage) &&
|
|
1251
1571
|
inspectionArg.mounts[0].destination === '/data' &&
|
|
@@ -1269,16 +1589,20 @@ export class ServiceManager {
|
|
|
1269
1589
|
);
|
|
1270
1590
|
}
|
|
1271
1591
|
|
|
1272
|
-
private async
|
|
1592
|
+
private async assertNoSymlinkedServiceDataPath(
|
|
1593
|
+
serviceArg: THardenedService,
|
|
1594
|
+
dataPathArg: string,
|
|
1595
|
+
): Promise<void> {
|
|
1596
|
+
const label = hardenedServiceSpecs[serviceArg].label;
|
|
1273
1597
|
const projectPath = plugins.path.resolve(process.cwd());
|
|
1274
|
-
const expectedPath = getServiceDataDirectory(projectPath,
|
|
1598
|
+
const expectedPath = getServiceDataDirectory(projectPath, serviceArg);
|
|
1275
1599
|
if (plugins.path.resolve(dataPathArg) !== expectedPath) {
|
|
1276
|
-
throw new Error(`Refusing non-canonical
|
|
1600
|
+
throw new Error(`Refusing non-canonical ${label} data path: ${dataPathArg}`);
|
|
1277
1601
|
}
|
|
1278
1602
|
for (const candidate of [projectPath, plugins.path.join(projectPath, '.nogit'), expectedPath]) {
|
|
1279
1603
|
try {
|
|
1280
1604
|
if ((await plugins.fs.lstat(candidate)).isSymbolicLink()) {
|
|
1281
|
-
throw new Error(`Refusing symbolic-link
|
|
1605
|
+
throw new Error(`Refusing symbolic-link ${label} path component: ${candidate}`);
|
|
1282
1606
|
}
|
|
1283
1607
|
} catch (error) {
|
|
1284
1608
|
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
|
|
@@ -1288,41 +1612,47 @@ export class ServiceManager {
|
|
|
1288
1612
|
}
|
|
1289
1613
|
}
|
|
1290
1614
|
|
|
1291
|
-
|
|
1615
|
+
/**
|
|
1616
|
+
* Create or prove the canonical data directory, then hand it to the image's
|
|
1617
|
+
* service user with private permissions through one fenced root operation.
|
|
1618
|
+
*/
|
|
1619
|
+
private async prepareHardenedDataDirectory(
|
|
1620
|
+
serviceArg: THardenedService,
|
|
1292
1621
|
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1293
1622
|
): Promise<Record<string, string>> {
|
|
1623
|
+
const spec = hardenedServiceSpecs[serviceArg];
|
|
1294
1624
|
const projectPath = plugins.path.resolve(process.cwd());
|
|
1295
|
-
const dataPath =
|
|
1296
|
-
await this.
|
|
1625
|
+
const dataPath = getServiceDataDirectory(projectPath, serviceArg);
|
|
1626
|
+
await this.assertNoSymlinkedServiceDataPath(serviceArg, dataPath);
|
|
1297
1627
|
const exists = await plugins.smartfs.directory(dataPath).exists();
|
|
1298
1628
|
const marker = await readServiceDataMarker(projectPath);
|
|
1299
|
-
if (exists && !markerProvesServiceDataDirectory(marker, projectPath,
|
|
1629
|
+
if (exists && !markerProvesServiceDataDirectory(marker, projectPath, serviceArg)) {
|
|
1300
1630
|
throw new Error(
|
|
1301
|
-
`Refusing existing
|
|
1631
|
+
`Refusing existing ${spec.label} data directory with unproven ownership: ${dataPath}`,
|
|
1302
1632
|
);
|
|
1303
1633
|
}
|
|
1304
1634
|
if (!exists) {
|
|
1305
|
-
await recordServiceDataDirectory(projectPath,
|
|
1635
|
+
await recordServiceDataDirectory(projectPath, serviceArg);
|
|
1306
1636
|
await this.createDataDirectory(dataPath);
|
|
1307
1637
|
}
|
|
1308
1638
|
|
|
1309
1639
|
const labels = getServiceContainerLabels({
|
|
1310
1640
|
projectPath,
|
|
1311
|
-
service:
|
|
1641
|
+
service: serviceArg,
|
|
1312
1642
|
dataPath,
|
|
1313
1643
|
});
|
|
1314
1644
|
await this.docker.runFencedBindOperation({
|
|
1315
|
-
image:
|
|
1645
|
+
image: spec.image,
|
|
1316
1646
|
hostPath: dataPath,
|
|
1317
1647
|
containerPath: '/data',
|
|
1318
1648
|
entrypoint: '/bin/sh',
|
|
1319
1649
|
command: [
|
|
1320
1650
|
'-c',
|
|
1321
|
-
|
|
1651
|
+
`/bin/chown -R ${spec.dataOwner} /data && /bin/chmod 0700 /data`,
|
|
1322
1652
|
],
|
|
1323
1653
|
labels: {
|
|
1324
1654
|
'git.zone.tool': serviceToolLabel,
|
|
1325
|
-
'git.zone.service':
|
|
1655
|
+
'git.zone.service': serviceArg,
|
|
1326
1656
|
'git.zone.project-path': projectPath,
|
|
1327
1657
|
'git.zone.data-path': plugins.path.resolve(dataPath),
|
|
1328
1658
|
'git.zone.safe-to-prune': 'false',
|
|
@@ -1330,32 +1660,41 @@ export class ServiceManager {
|
|
|
1330
1660
|
},
|
|
1331
1661
|
executionOptions: dockerTimeoutArg,
|
|
1332
1662
|
rollbackTimeoutMs: objectStorageRollbackTimeoutMs,
|
|
1333
|
-
recoverablePredecessors:
|
|
1334
|
-
{
|
|
1335
|
-
entrypoint: '/bin/chown',
|
|
1336
|
-
command: ['-R', 'objectstorage:objectstorage', '/data'],
|
|
1337
|
-
},
|
|
1338
|
-
],
|
|
1663
|
+
recoverablePredecessors: spec.recoverablePredecessors,
|
|
1339
1664
|
});
|
|
1340
1665
|
return labels;
|
|
1341
1666
|
}
|
|
1342
1667
|
|
|
1343
|
-
private async
|
|
1668
|
+
private async prepareObjectStorageDataDirectory(
|
|
1669
|
+
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1670
|
+
): Promise<Record<string, string>> {
|
|
1671
|
+
return this.prepareHardenedDataDirectory('objectstorage', dockerTimeoutArg);
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
/** An exactly owned container proves its canonical data directory. */
|
|
1675
|
+
private async repairServiceMarkerFromOwnedContainer(
|
|
1676
|
+
serviceArg: THardenedService,
|
|
1677
|
+
dataPathArg: string,
|
|
1678
|
+
): Promise<void> {
|
|
1344
1679
|
const projectPath = plugins.path.resolve(process.cwd());
|
|
1345
|
-
await this.
|
|
1680
|
+
await this.assertNoSymlinkedServiceDataPath(serviceArg, dataPathArg);
|
|
1346
1681
|
if (!(await plugins.smartfs.directory(dataPathArg).exists())) {
|
|
1347
1682
|
throw new Error(
|
|
1348
|
-
`Refusing owned
|
|
1683
|
+
`Refusing owned ${hardenedServiceSpecs[serviceArg].label} container with a missing data directory: ${dataPathArg}`,
|
|
1349
1684
|
);
|
|
1350
1685
|
}
|
|
1351
1686
|
const marker = await readServiceDataMarker(projectPath);
|
|
1352
|
-
if (!markerProvesServiceDataDirectory(marker, projectPath,
|
|
1687
|
+
if (!markerProvesServiceDataDirectory(marker, projectPath, serviceArg)) {
|
|
1353
1688
|
// recordServiceDataDirectory rejects an invalid/foreign marker, so only an
|
|
1354
1689
|
// absent or valid mergeable claim is repaired from exact container labels.
|
|
1355
|
-
await recordServiceDataDirectory(projectPath,
|
|
1690
|
+
await recordServiceDataDirectory(projectPath, serviceArg);
|
|
1356
1691
|
}
|
|
1357
1692
|
}
|
|
1358
1693
|
|
|
1694
|
+
private async repairObjectStorageMarkerFromOwnedContainer(dataPathArg: string): Promise<void> {
|
|
1695
|
+
await this.repairServiceMarkerFromOwnedContainer('objectstorage', dataPathArg);
|
|
1696
|
+
}
|
|
1697
|
+
|
|
1359
1698
|
private async assertNoLegacyMinioResources(
|
|
1360
1699
|
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1361
1700
|
): Promise<void> {
|
|
@@ -1483,7 +1822,7 @@ export class ServiceManager {
|
|
|
1483
1822
|
private async createObjectStorageContainer(
|
|
1484
1823
|
labelsArg: Record<string, string>,
|
|
1485
1824
|
dockerTimeoutArg: () => { timeoutMs: number },
|
|
1486
|
-
): Promise<
|
|
1825
|
+
): Promise<IManagedContainerInspection> {
|
|
1487
1826
|
const config = this.config.getConfig();
|
|
1488
1827
|
const containers = this.config.getContainerNames();
|
|
1489
1828
|
const directories = this.config.getDataDirectories();
|
|
@@ -1586,7 +1925,7 @@ export class ServiceManager {
|
|
|
1586
1925
|
containers.objectstorage,
|
|
1587
1926
|
dockerTimeout,
|
|
1588
1927
|
);
|
|
1589
|
-
let initialInspection:
|
|
1928
|
+
let initialInspection: IManagedContainerInspection | undefined;
|
|
1590
1929
|
if (existingId) {
|
|
1591
1930
|
initialInspection = await this.inspectObjectStorageContainer(
|
|
1592
1931
|
existingId,
|
|
@@ -1624,7 +1963,7 @@ export class ServiceManager {
|
|
|
1624
1963
|
const needsRecreate =
|
|
1625
1964
|
!!initialInspection &&
|
|
1626
1965
|
!this.objectStorageConfigurationMatches(initialInspection, expectedLabels);
|
|
1627
|
-
let activeInspection:
|
|
1966
|
+
let activeInspection: IManagedContainerInspection;
|
|
1628
1967
|
if (!initialInspection || needsRecreate) {
|
|
1629
1968
|
const labels = await this.prepareObjectStorageDataDirectory(dockerTimeout);
|
|
1630
1969
|
if (initialInspection) {
|
|
@@ -1835,20 +2174,39 @@ export class ServiceManager {
|
|
|
1835
2174
|
|
|
1836
2175
|
private async stopMongoDBLocked(): Promise<void> {
|
|
1837
2176
|
await this.config.reloadFromDisk();
|
|
1838
|
-
logger.log('note', '📦 MongoDB:');
|
|
1839
|
-
|
|
2177
|
+
logger.log('note', '📦 MongoDB (NoSQLDB):');
|
|
1840
2178
|
const containers = this.config.getContainerNames();
|
|
1841
|
-
const
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
2179
|
+
const directories = this.config.getDataDirectories();
|
|
2180
|
+
const labels = getServiceContainerLabels({
|
|
2181
|
+
projectPath: process.cwd(),
|
|
2182
|
+
service: 'mongodb',
|
|
2183
|
+
dataPath: directories.nosqldb,
|
|
2184
|
+
});
|
|
2185
|
+
const timeout = () => ({ timeoutMs: 60_000 });
|
|
2186
|
+
const id = await this.discoverContainerIdByExactName(containers.nosqldb, timeout);
|
|
2187
|
+
const inspection = id
|
|
2188
|
+
? await this.revalidateNoSqlDbContainer(id, containers.nosqldb, labels, timeout)
|
|
2189
|
+
: undefined;
|
|
2190
|
+
if (inspection?.running) {
|
|
2191
|
+
await this.docker.execArgv(['stop', inspection.id], timeout());
|
|
2192
|
+
logger.log('ok', ' Stopped ✓');
|
|
1849
2193
|
} else {
|
|
1850
2194
|
logger.log('note', ' Not running');
|
|
1851
2195
|
}
|
|
2196
|
+
await this.stopLegacyMongoContainer();
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
/** Stop a proven-owned, unmigrated legacy mongod container by immutable id. */
|
|
2200
|
+
private async stopLegacyMongoContainer(): Promise<void> {
|
|
2201
|
+
const assessment = await this.createLegacyMongoMigration().assessPending();
|
|
2202
|
+
if (assessment.kind !== 'migratable' || !assessment.source.container?.running) {
|
|
2203
|
+
return;
|
|
2204
|
+
}
|
|
2205
|
+
await this.docker.execArgv(['stop', assessment.source.container.id], { timeoutMs: 60_000 });
|
|
2206
|
+
logger.log(
|
|
2207
|
+
'ok',
|
|
2208
|
+
` Legacy MongoDB container ${assessment.source.container.name} stopped ✓ (migrates on next start)`,
|
|
2209
|
+
);
|
|
1852
2210
|
}
|
|
1853
2211
|
|
|
1854
2212
|
/** Stop only an exactly owned canonical ObjectStorage container. */
|
|
@@ -1925,6 +2283,51 @@ export class ServiceManager {
|
|
|
1925
2283
|
* --json`; consumers such as test suites can read a connection string from it
|
|
1926
2284
|
* without scraping human output or re-deriving it from `.nogit/env.json`.
|
|
1927
2285
|
*/
|
|
2286
|
+
/** Status of a label-owned service container, resolved by immutable id. */
|
|
2287
|
+
private async describeManagedContainer(optionsArg: {
|
|
2288
|
+
service: THardenedService;
|
|
2289
|
+
containerName: string;
|
|
2290
|
+
dataPath: string;
|
|
2291
|
+
port: string;
|
|
2292
|
+
connectionString: string;
|
|
2293
|
+
}): Promise<IServiceStatus> {
|
|
2294
|
+
const timeout = () => ({ timeoutMs: 60_000 });
|
|
2295
|
+
const expectedLabels = getServiceContainerLabels({
|
|
2296
|
+
projectPath: process.cwd(),
|
|
2297
|
+
service: optionsArg.service,
|
|
2298
|
+
dataPath: optionsArg.dataPath,
|
|
2299
|
+
});
|
|
2300
|
+
const id = await this.discoverContainerIdByExactName(optionsArg.containerName, timeout);
|
|
2301
|
+
let status: ContainerStatus = 'not_exists';
|
|
2302
|
+
let ownership: IServiceStatus['ownership'] = 'absent';
|
|
2303
|
+
if (id) {
|
|
2304
|
+
const identity = await this.inspectManagedContainerIdentity(
|
|
2305
|
+
id,
|
|
2306
|
+
optionsArg.containerName,
|
|
2307
|
+
hardenedServiceSpecs[optionsArg.service].label,
|
|
2308
|
+
timeout,
|
|
2309
|
+
);
|
|
2310
|
+
status = identity.running ? 'running' : 'stopped';
|
|
2311
|
+
ownership = hasExactServiceContainerLabels(identity.labels, expectedLabels)
|
|
2312
|
+
? 'owned'
|
|
2313
|
+
: 'conflict';
|
|
2314
|
+
}
|
|
2315
|
+
const dataExists = await plugins.smartfs.directory(optionsArg.dataPath).exists();
|
|
2316
|
+
return {
|
|
2317
|
+
service: optionsArg.service,
|
|
2318
|
+
enabled: this.isServiceEnabled(optionsArg.service),
|
|
2319
|
+
container: optionsArg.containerName,
|
|
2320
|
+
status,
|
|
2321
|
+
port: optionsArg.port,
|
|
2322
|
+
connectionString: optionsArg.connectionString,
|
|
2323
|
+
dataDirectory: optionsArg.dataPath,
|
|
2324
|
+
dataSizeBytes: dataExists ? await helpers.getDirectorySize(optionsArg.dataPath) : 0,
|
|
2325
|
+
portAvailable:
|
|
2326
|
+
status === 'not_exists' ? await helpers.isPortAvailable(parseInt(optionsArg.port)) : null,
|
|
2327
|
+
ownership,
|
|
2328
|
+
};
|
|
2329
|
+
}
|
|
2330
|
+
|
|
1928
2331
|
public async collectStatus(): Promise<IServicesStatus> {
|
|
1929
2332
|
const config = this.config.getConfig();
|
|
1930
2333
|
const containers = this.config.getContainerNames();
|
|
@@ -1953,62 +2356,24 @@ export class ServiceManager {
|
|
|
1953
2356
|
};
|
|
1954
2357
|
};
|
|
1955
2358
|
|
|
1956
|
-
const mongo = await
|
|
1957
|
-
'mongodb',
|
|
1958
|
-
containers.
|
|
1959
|
-
directories.
|
|
1960
|
-
config.MONGODB_PORT,
|
|
1961
|
-
this.config.getMongoConnectionString(),
|
|
1962
|
-
);
|
|
2359
|
+
const mongo = await this.describeManagedContainer({
|
|
2360
|
+
service: 'mongodb',
|
|
2361
|
+
containerName: containers.nosqldb,
|
|
2362
|
+
dataPath: directories.nosqldb,
|
|
2363
|
+
port: config.MONGODB_PORT,
|
|
2364
|
+
connectionString: this.config.getMongoConnectionString(),
|
|
2365
|
+
});
|
|
1963
2366
|
mongo.authEnabled = this.config.isMongoAuthEnabled();
|
|
1964
2367
|
mongo.authSource = this.config.getMongoAuthSource();
|
|
1965
2368
|
|
|
1966
|
-
const
|
|
1967
|
-
directories.objectstorage,
|
|
1968
|
-
).exists();
|
|
1969
|
-
const objectStorageLabels = getServiceContainerLabels({
|
|
1970
|
-
projectPath: process.cwd(),
|
|
2369
|
+
const objectstorage = await this.describeManagedContainer({
|
|
1971
2370
|
service: 'objectstorage',
|
|
2371
|
+
containerName: containers.objectstorage,
|
|
1972
2372
|
dataPath: directories.objectstorage,
|
|
1973
|
-
});
|
|
1974
|
-
const objectStorageId = await this.discoverContainerIdByExactName(
|
|
1975
|
-
containers.objectstorage,
|
|
1976
|
-
() => ({ timeoutMs: 60_000 }),
|
|
1977
|
-
);
|
|
1978
|
-
let objectStorageStatus: ContainerStatus = 'not_exists';
|
|
1979
|
-
let objectStorageOwnership: IServiceStatus['ownership'] = 'absent';
|
|
1980
|
-
if (objectStorageId) {
|
|
1981
|
-
const inspection = await this.inspectObjectStorageIdentity(
|
|
1982
|
-
objectStorageId,
|
|
1983
|
-
containers.objectstorage,
|
|
1984
|
-
() => ({ timeoutMs: 60_000 }),
|
|
1985
|
-
);
|
|
1986
|
-
objectStorageStatus = inspection.running ? 'running' : 'stopped';
|
|
1987
|
-
objectStorageOwnership = hasExactServiceContainerLabels(
|
|
1988
|
-
inspection.labels,
|
|
1989
|
-
objectStorageLabels,
|
|
1990
|
-
)
|
|
1991
|
-
? 'owned'
|
|
1992
|
-
: 'conflict';
|
|
1993
|
-
}
|
|
1994
|
-
const objectstorage: IServiceStatus = {
|
|
1995
|
-
service: 'objectstorage',
|
|
1996
|
-
enabled: this.isServiceEnabled('objectstorage'),
|
|
1997
|
-
container: containers.objectstorage,
|
|
1998
|
-
status: objectStorageStatus,
|
|
1999
2373
|
port: config.S3_PORT,
|
|
2000
2374
|
connectionString: `http://127.0.0.1:${config.S3_PORT}`,
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
dataSizeBytes: objectStorageDataExists
|
|
2004
|
-
? await helpers.getDirectorySize(directories.objectstorage)
|
|
2005
|
-
: 0,
|
|
2006
|
-
portAvailable:
|
|
2007
|
-
objectStorageStatus === 'not_exists'
|
|
2008
|
-
? await helpers.isPortAvailable(parseInt(config.S3_PORT))
|
|
2009
|
-
: null,
|
|
2010
|
-
ownership: objectStorageOwnership,
|
|
2011
|
-
};
|
|
2375
|
+
});
|
|
2376
|
+
objectstorage.managementUi = `http://127.0.0.1:${config.S3_UI_PORT}`;
|
|
2012
2377
|
const elasticsearch = await describe(
|
|
2013
2378
|
'elasticsearch',
|
|
2014
2379
|
containers.elasticsearch,
|
|
@@ -2027,6 +2392,13 @@ export class ServiceManager {
|
|
|
2027
2392
|
containers.legacyMinio,
|
|
2028
2393
|
() => ({ timeoutMs: 60_000 }),
|
|
2029
2394
|
);
|
|
2395
|
+
const legacyMongoName = registryProject?.legacy?.mongoContainer || containers.legacyMongo;
|
|
2396
|
+
const legacyMongoExists = await this.discoverContainerIdByExactName(
|
|
2397
|
+
legacyMongoName,
|
|
2398
|
+
() => ({ timeoutMs: 60_000 }),
|
|
2399
|
+
);
|
|
2400
|
+
const legacyMongoDataDirectory = getLegacyMongoDataDirectory(projectPath);
|
|
2401
|
+
const mongoMigrationDirectory = getLegacyMongoMigrationEvidenceRoot(projectPath);
|
|
2030
2402
|
|
|
2031
2403
|
return {
|
|
2032
2404
|
project: {
|
|
@@ -2043,6 +2415,13 @@ export class ServiceManager {
|
|
|
2043
2415
|
: {}),
|
|
2044
2416
|
minioDataDirectory: legacyDataDirectory,
|
|
2045
2417
|
minioDataExists: await plugins.smartfs.directory(legacyDataDirectory).exists(),
|
|
2418
|
+
...(legacyMongoExists ? { mongoContainer: legacyMongoName } : {}),
|
|
2419
|
+
mongoDataDirectory: legacyMongoDataDirectory,
|
|
2420
|
+
mongoDataExists: await plugins.smartfs.directory(legacyMongoDataDirectory).exists(),
|
|
2421
|
+
mongoMigrationDirectory,
|
|
2422
|
+
mongoMigrationDirectoryExists: await plugins.smartfs
|
|
2423
|
+
.directory(mongoMigrationDirectory)
|
|
2424
|
+
.exists(),
|
|
2046
2425
|
},
|
|
2047
2426
|
totalDataBytes:
|
|
2048
2427
|
mongo.dataSizeBytes + objectstorage.dataSizeBytes + elasticsearch.dataSizeBytes,
|
|
@@ -2062,43 +2441,43 @@ export class ServiceManager {
|
|
|
2062
2441
|
logger.log('info', `Project: ${config.PROJECT_NAME}`);
|
|
2063
2442
|
console.log();
|
|
2064
2443
|
|
|
2065
|
-
// MongoDB status
|
|
2066
|
-
const
|
|
2067
|
-
|
|
2444
|
+
// MongoDB (NoSQLDB) status
|
|
2445
|
+
const mongo = structuredStatus.services.mongodb;
|
|
2446
|
+
if (mongo.ownership === 'conflict') {
|
|
2447
|
+
logger.log(
|
|
2448
|
+
'error',
|
|
2449
|
+
`📦 MongoDB (NoSQLDB): ownership conflict for ${mongo.container}; refusing to treat it as managed`,
|
|
2450
|
+
);
|
|
2451
|
+
} else switch (mongo.status) {
|
|
2068
2452
|
case 'running':
|
|
2069
|
-
logger.log('ok', '📦 MongoDB: 🟢 Running');
|
|
2070
|
-
logger.log('info', ` ├─ Container: ${containers.
|
|
2071
|
-
logger.log('info', ` ├─ Port:
|
|
2453
|
+
logger.log('ok', '📦 MongoDB (NoSQLDB): 🟢 Running');
|
|
2454
|
+
logger.log('info', ` ├─ Container: ${containers.nosqldb}`);
|
|
2455
|
+
logger.log('info', ` ├─ Port: 127.0.0.1:${config.MONGODB_PORT}`);
|
|
2072
2456
|
if (!this.config.isMongoAuthEnabled()) {
|
|
2073
|
-
logger.log(
|
|
2074
|
-
'note',
|
|
2075
|
-
` ├─ ⚠️ Auth: DISABLED (loopback only), ${this.describeMongoAuthSource()}`,
|
|
2076
|
-
);
|
|
2457
|
+
logger.log('note', ` ├─ ⚠️ Auth: DISABLED, ${this.describeMongoAuthSource()}`);
|
|
2077
2458
|
}
|
|
2078
|
-
logger.log('info', `
|
|
2079
|
-
|
|
2080
|
-
// Show Compass connection string
|
|
2081
|
-
const networkIp = await helpers.getLocalNetworkIp();
|
|
2082
|
-
const compassString = `mongodb://${config.MONGODB_USER}:${config.MONGODB_PASS}@${networkIp}:${config.MONGODB_PORT}/${config.MONGODB_NAME}?authSource=admin`;
|
|
2083
|
-
logger.log('ok', ` └─ Compass: ${compassString}`);
|
|
2459
|
+
logger.log('info', ` └─ Connection: ${this.config.getMongoConnectionString()}`);
|
|
2084
2460
|
break;
|
|
2085
2461
|
case 'stopped':
|
|
2086
|
-
logger.log('note', '📦 MongoDB: 🟡 Stopped');
|
|
2087
|
-
logger.log('info', ` ├─ Container: ${containers.
|
|
2462
|
+
logger.log('note', '📦 MongoDB (NoSQLDB): 🟡 Stopped');
|
|
2463
|
+
logger.log('info', ` ├─ Container: ${containers.nosqldb}`);
|
|
2088
2464
|
logger.log('info', ` └─ Port: ${config.MONGODB_PORT}`);
|
|
2089
2465
|
break;
|
|
2090
2466
|
case 'not_exists':
|
|
2091
|
-
logger.log('info', '📦 MongoDB: ⚪ Not installed');
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
const mongoAvailable = await helpers.isPortAvailable(mongoPort);
|
|
2095
|
-
if (!mongoAvailable) {
|
|
2096
|
-
logger.log('error', ` └─ ⚠️ Port ${mongoPort} is in use by another process`);
|
|
2467
|
+
logger.log('info', '📦 MongoDB (NoSQLDB): ⚪ Not installed');
|
|
2468
|
+
if (mongo.portAvailable === false) {
|
|
2469
|
+
logger.log('error', ` └─ ⚠️ Port ${config.MONGODB_PORT} is in use by another process`);
|
|
2097
2470
|
} else {
|
|
2098
|
-
logger.log('info', ` └─ Port ${
|
|
2471
|
+
logger.log('info', ` └─ Port ${config.MONGODB_PORT} is available`);
|
|
2099
2472
|
}
|
|
2100
2473
|
break;
|
|
2101
2474
|
}
|
|
2475
|
+
if (structuredStatus.legacy.mongoContainer) {
|
|
2476
|
+
logger.log(
|
|
2477
|
+
'note',
|
|
2478
|
+
` Legacy MongoDB container ${structuredStatus.legacy.mongoContainer} is not migrated yet; \`gitzone services start mongo\` migrates its data to NoSQLDB.`,
|
|
2479
|
+
);
|
|
2480
|
+
}
|
|
2102
2481
|
|
|
2103
2482
|
// ObjectStorage status
|
|
2104
2483
|
const objectstorage = structuredStatus.services.objectstorage;
|
|
@@ -2196,7 +2575,7 @@ export class ServiceManager {
|
|
|
2196
2575
|
public async showDiskUsage(): Promise<void> {
|
|
2197
2576
|
const directories = this.config.getDataDirectories();
|
|
2198
2577
|
const entries: Array<{ label: string; path: string }> = [
|
|
2199
|
-
{ label: 'MongoDB', path: directories.
|
|
2578
|
+
{ label: 'MongoDB (NoSQLDB)', path: directories.nosqldb },
|
|
2200
2579
|
{ label: 'S3/ObjectStorage', path: directories.objectstorage },
|
|
2201
2580
|
{ label: 'Elasticsearch', path: directories.elasticsearch },
|
|
2202
2581
|
];
|
|
@@ -2225,6 +2604,18 @@ export class ServiceManager {
|
|
|
2225
2604
|
` Legacy MinIO data is preserved and excluded from cleanup: ${legacyDataPath}`,
|
|
2226
2605
|
);
|
|
2227
2606
|
}
|
|
2607
|
+
const preservedMongoPaths = [
|
|
2608
|
+
getLegacyMongoDataDirectory(process.cwd()),
|
|
2609
|
+
getLegacyMongoMigrationEvidenceRoot(process.cwd()),
|
|
2610
|
+
];
|
|
2611
|
+
for (const preservedPath of preservedMongoPaths) {
|
|
2612
|
+
if (await plugins.smartfs.directory(preservedPath).exists()) {
|
|
2613
|
+
logger.log(
|
|
2614
|
+
'note',
|
|
2615
|
+
` Legacy MongoDB data is preserved and excluded from cleanup: ${preservedPath}`,
|
|
2616
|
+
);
|
|
2617
|
+
}
|
|
2618
|
+
}
|
|
2228
2619
|
if (total === 0 && !legacyDataExists) {
|
|
2229
2620
|
return;
|
|
2230
2621
|
}
|
|
@@ -2245,13 +2636,14 @@ export class ServiceManager {
|
|
|
2245
2636
|
logger.log('info', `Project: ${config.PROJECT_NAME}`);
|
|
2246
2637
|
console.log();
|
|
2247
2638
|
|
|
2248
|
-
logger.log('note', 'MongoDB:');
|
|
2639
|
+
logger.log('note', 'MongoDB (NoSQLDB):');
|
|
2249
2640
|
logger.log('info', ` Host: ${config.MONGODB_HOST}:${config.MONGODB_PORT}`);
|
|
2250
2641
|
logger.log('info', ` Database: ${config.MONGODB_NAME}`);
|
|
2251
2642
|
logger.log('info', ` User: ${config.MONGODB_USER}`);
|
|
2252
2643
|
logger.log('info', ' Password: ***');
|
|
2253
|
-
logger.log('info',
|
|
2254
|
-
logger.log('info', `
|
|
2644
|
+
logger.log('info', ' Admin Password: ***');
|
|
2645
|
+
logger.log('info', ` Container: ${this.config.getContainerNames().nosqldb}`);
|
|
2646
|
+
logger.log('info', ` Data: ${this.config.getDataDirectories().nosqldb}`);
|
|
2255
2647
|
logger.log('info', ` Connection: ${this.config.getMongoConnectionString()}`);
|
|
2256
2648
|
|
|
2257
2649
|
console.log();
|
|
@@ -2281,30 +2673,34 @@ export class ServiceManager {
|
|
|
2281
2673
|
}
|
|
2282
2674
|
|
|
2283
2675
|
/**
|
|
2284
|
-
* Show MongoDB Compass connection string
|
|
2676
|
+
* Show a MongoDB Compass connection string for the NoSQLDB service.
|
|
2285
2677
|
*/
|
|
2286
2678
|
public async showCompassConnection(): Promise<void> {
|
|
2287
2679
|
helpers.printHeader('MongoDB Compass Connection');
|
|
2288
|
-
|
|
2289
2680
|
const config = this.config.getConfig();
|
|
2290
|
-
const
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
logger.log('info', 'MongoDB Compass is a GUI tool for MongoDB. To connect:');
|
|
2681
|
+
const connectionString = this.config.getMongoConnectionString();
|
|
2682
|
+
|
|
2683
|
+
logger.log('info', 'MongoDB Compass connects to NoSQLDB through the MongoDB wire protocol:');
|
|
2295
2684
|
console.log();
|
|
2296
2685
|
logger.log('info', '1. Download MongoDB Compass from:');
|
|
2297
2686
|
logger.log('info', ' https://www.mongodb.com/products/compass');
|
|
2298
2687
|
console.log();
|
|
2299
|
-
logger.log('info', '2. Open Compass and paste this connection string:');
|
|
2688
|
+
logger.log('info', '2. Open Compass on this machine and paste this connection string:');
|
|
2300
2689
|
logger.log('ok', ` ${connectionString}`);
|
|
2301
2690
|
console.log();
|
|
2302
2691
|
logger.log('note', 'Connection Details:');
|
|
2303
|
-
logger.log('info', `
|
|
2692
|
+
logger.log('info', ` Host: 127.0.0.1 (published on loopback only)`);
|
|
2304
2693
|
logger.log('info', ` Port: ${config.MONGODB_PORT}`);
|
|
2305
2694
|
logger.log('info', ` Database: ${config.MONGODB_NAME}`);
|
|
2306
|
-
|
|
2307
|
-
|
|
2695
|
+
if (this.config.isMongoAuthEnabled()) {
|
|
2696
|
+
logger.log('info', ` Username: ${config.MONGODB_USER}`);
|
|
2697
|
+
logger.log('info', ' Auth Source: admin');
|
|
2698
|
+
}
|
|
2699
|
+
console.log();
|
|
2700
|
+
logger.log(
|
|
2701
|
+
'note',
|
|
2702
|
+
'NoSQLDB does not implement every MongoDB administrative command (for example currentOp, hostInfo, getLog); Compass panels that rely on them stay empty.',
|
|
2703
|
+
);
|
|
2308
2704
|
}
|
|
2309
2705
|
|
|
2310
2706
|
/**
|
|
@@ -2316,12 +2712,32 @@ export class ServiceManager {
|
|
|
2316
2712
|
switch (service) {
|
|
2317
2713
|
case 'mongo':
|
|
2318
2714
|
case 'mongodb':
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
const
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2715
|
+
{
|
|
2716
|
+
const directories = this.config.getDataDirectories();
|
|
2717
|
+
const labels = getServiceContainerLabels({
|
|
2718
|
+
projectPath: process.cwd(),
|
|
2719
|
+
service: 'mongodb',
|
|
2720
|
+
dataPath: directories.nosqldb,
|
|
2721
|
+
});
|
|
2722
|
+
const timeout = () => ({ timeoutMs: 60_000 });
|
|
2723
|
+
const id = await this.discoverContainerIdByExactName(containers.nosqldb, timeout);
|
|
2724
|
+
if (!id) {
|
|
2725
|
+
logger.log('note', 'MongoDB (NoSQLDB) container does not exist');
|
|
2726
|
+
break;
|
|
2727
|
+
}
|
|
2728
|
+
await this.revalidateNoSqlDbContainer(id, containers.nosqldb, labels, timeout);
|
|
2729
|
+
helpers.printHeader(`MongoDB (NoSQLDB) Logs (last ${lines} lines)`);
|
|
2730
|
+
const result = await this.docker.execArgv(
|
|
2731
|
+
['logs', '--tail', String(lines), id],
|
|
2732
|
+
timeout(),
|
|
2733
|
+
);
|
|
2734
|
+
const config = this.config.getConfig();
|
|
2735
|
+
console.log(
|
|
2736
|
+
redactSecrets(`${result.stdout}${result.stderr}`, [
|
|
2737
|
+
config.MONGODB_PASS,
|
|
2738
|
+
config.MONGODB_ADMIN_PASSWORD,
|
|
2739
|
+
]),
|
|
2740
|
+
);
|
|
2325
2741
|
}
|
|
2326
2742
|
break;
|
|
2327
2743
|
|
|
@@ -2355,18 +2771,13 @@ export class ServiceManager {
|
|
|
2355
2771
|
timeout(),
|
|
2356
2772
|
);
|
|
2357
2773
|
const config = this.config.getConfig();
|
|
2358
|
-
|
|
2359
|
-
|
|
2360
|
-
new Set([
|
|
2774
|
+
console.log(
|
|
2775
|
+
redactSecrets(`${result.stdout}${result.stderr}`, [
|
|
2361
2776
|
config.S3_ACCESSKEY,
|
|
2362
2777
|
config.S3_SECRETKEY,
|
|
2363
2778
|
config.S3_ADMIN_PASSWORD,
|
|
2364
|
-
]
|
|
2365
|
-
)
|
|
2366
|
-
for (const secret of secrets) {
|
|
2367
|
-
output = output.split(secret).join('***');
|
|
2368
|
-
}
|
|
2369
|
-
console.log(output);
|
|
2779
|
+
]),
|
|
2780
|
+
);
|
|
2370
2781
|
}
|
|
2371
2782
|
break;
|
|
2372
2783
|
|
|
@@ -2432,10 +2843,10 @@ export class ServiceManager {
|
|
|
2432
2843
|
registered: registeredArg || existing?.registered === true,
|
|
2433
2844
|
});
|
|
2434
2845
|
};
|
|
2435
|
-
addTarget('mongodb', containers.
|
|
2846
|
+
addTarget('mongodb', containers.nosqldb, false);
|
|
2436
2847
|
addTarget('objectstorage', containers.objectstorage, false);
|
|
2437
2848
|
addTarget('elasticsearch', containers.elasticsearch, false);
|
|
2438
|
-
addTarget('mongodb', registryProject?.containers.
|
|
2849
|
+
addTarget('mongodb', registryProject?.containers.nosqldb, true);
|
|
2439
2850
|
addTarget('objectstorage', registryProject?.containers.objectstorage, true);
|
|
2440
2851
|
addTarget('elasticsearch', registryProject?.containers.elasticsearch, true);
|
|
2441
2852
|
|
|
@@ -2460,7 +2871,7 @@ export class ServiceManager {
|
|
|
2460
2871
|
service: target.service,
|
|
2461
2872
|
dataPath:
|
|
2462
2873
|
target.service === 'mongodb'
|
|
2463
|
-
? directories.
|
|
2874
|
+
? directories.nosqldb
|
|
2464
2875
|
: target.service === 'objectstorage'
|
|
2465
2876
|
? directories.objectstorage
|
|
2466
2877
|
: directories.elasticsearch,
|
|
@@ -2472,7 +2883,7 @@ export class ServiceManager {
|
|
|
2472
2883
|
const registryClaims = Object.entries(registryProjects).flatMap(
|
|
2473
2884
|
([registeredPathArg, projectArg]) => {
|
|
2474
2885
|
const claims: Array<{ projectPath: string; service: TServiceName }> = [];
|
|
2475
|
-
if (projectArg.containers.
|
|
2886
|
+
if (projectArg.containers.nosqldb === target.name) {
|
|
2476
2887
|
claims.push({ projectPath: plugins.path.resolve(registeredPathArg), service: 'mongodb' });
|
|
2477
2888
|
}
|
|
2478
2889
|
if (projectArg.containers.objectstorage === target.name) {
|
|
@@ -2493,7 +2904,7 @@ export class ServiceManager {
|
|
|
2493
2904
|
const registryOwned =
|
|
2494
2905
|
!hasGitZoneLabels &&
|
|
2495
2906
|
target.registered &&
|
|
2496
|
-
target.service
|
|
2907
|
+
target.service === 'elasticsearch' &&
|
|
2497
2908
|
registryClaims.length === 1 &&
|
|
2498
2909
|
registryClaims[0].projectPath === projectPath &&
|
|
2499
2910
|
registryClaims[0].service === target.service;
|
|
@@ -2509,11 +2920,17 @@ export class ServiceManager {
|
|
|
2509
2920
|
) {
|
|
2510
2921
|
await this.repairObjectStorageMarkerFromOwnedContainer(directories.objectstorage);
|
|
2511
2922
|
}
|
|
2923
|
+
if (
|
|
2924
|
+
plannedRemovals.some((removalArg) => removalArg.service === 'mongodb') &&
|
|
2925
|
+
(await plugins.smartfs.directory(directories.nosqldb).exists())
|
|
2926
|
+
) {
|
|
2927
|
+
await this.repairServiceMarkerFromOwnedContainer('mongodb', directories.nosqldb);
|
|
2928
|
+
}
|
|
2512
2929
|
for (const removal of plannedRemovals) {
|
|
2513
2930
|
await this.docker.execArgv(['rm', '-f', removal.inspection.id], { timeoutMs: 60_000 });
|
|
2514
2931
|
const label =
|
|
2515
2932
|
removal.service === 'mongodb'
|
|
2516
|
-
? 'MongoDB'
|
|
2933
|
+
? 'MongoDB (NoSQLDB)'
|
|
2517
2934
|
: removal.service === 'objectstorage'
|
|
2518
2935
|
? 'S3/ObjectStorage'
|
|
2519
2936
|
: 'Elasticsearch';
|
|
@@ -2539,7 +2956,10 @@ export class ServiceManager {
|
|
|
2539
2956
|
`label=git.zone.project-path=${projectPath}`,
|
|
2540
2957
|
]);
|
|
2541
2958
|
let registeredContainerExists = labeledIds.length > 0;
|
|
2542
|
-
for (const containerName of
|
|
2959
|
+
for (const containerName of [
|
|
2960
|
+
...Object.values(currentProject.containers),
|
|
2961
|
+
currentProject.legacy?.mongoContainer,
|
|
2962
|
+
]) {
|
|
2543
2963
|
if (!containerName) continue;
|
|
2544
2964
|
if (
|
|
2545
2965
|
await this.discoverContainerIdByExactName(
|
|
@@ -2577,7 +2997,7 @@ export class ServiceManager {
|
|
|
2577
2997
|
public async cleanData(): Promise<void> {
|
|
2578
2998
|
const directories = this.config.getDataDirectories();
|
|
2579
2999
|
const targets: Array<{ label: string; service: TServiceName; path: string }> = [
|
|
2580
|
-
{ label: 'MongoDB', service: 'mongodb', path: directories.
|
|
3000
|
+
{ label: 'MongoDB (NoSQLDB)', service: 'mongodb', path: directories.nosqldb },
|
|
2581
3001
|
{ label: 'S3/ObjectStorage', service: 'objectstorage', path: directories.objectstorage },
|
|
2582
3002
|
{ label: 'Elasticsearch', service: 'elasticsearch', path: directories.elasticsearch },
|
|
2583
3003
|
];
|
|
@@ -2602,12 +3022,14 @@ export class ServiceManager {
|
|
|
2602
3022
|
if (!cleaned) {
|
|
2603
3023
|
logger.log('note', ' No data to clean');
|
|
2604
3024
|
}
|
|
2605
|
-
const legacyDataPath
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
)
|
|
3025
|
+
for (const [label, legacyDataPath] of [
|
|
3026
|
+
['MinIO', getLegacyMinioDataDirectory(process.cwd())],
|
|
3027
|
+
['MongoDB', getLegacyMongoDataDirectory(process.cwd())],
|
|
3028
|
+
['MongoDB migration', getLegacyMongoMigrationEvidenceRoot(process.cwd())],
|
|
3029
|
+
] as const) {
|
|
3030
|
+
if (await plugins.smartfs.directory(legacyDataPath).exists()) {
|
|
3031
|
+
logger.log('note', ` Legacy ${label} data was preserved and not modified: ${legacyDataPath}`);
|
|
3032
|
+
}
|
|
2611
3033
|
}
|
|
2612
3034
|
}
|
|
2613
3035
|
|
|
@@ -2669,6 +3091,7 @@ export class ServiceManager {
|
|
|
2669
3091
|
private async reconfigureLocked(): Promise<void> {
|
|
2670
3092
|
await this.config.reloadFromDisk();
|
|
2671
3093
|
await this.assertNoLegacyMinioResources(() => ({ timeoutMs: 60_000 }));
|
|
3094
|
+
await this.assertMongoStartupAllowed();
|
|
2672
3095
|
|
|
2673
3096
|
// Stop existing containers
|
|
2674
3097
|
logger.log('note', '🛑 Stopping existing containers...');
|