-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtb-config-src.js
More file actions
1677 lines (1487 loc) · 68.2 KB
/
tb-config-src.js
File metadata and controls
1677 lines (1487 loc) · 68.2 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec, spawn, execFile } = require('child_process');
const os = require('os');
const { resolveAppContext, resolveAppRoot } = require('./src/server/app-context');
const { createAuthService } = require('./src/server/auth/session');
const { createEnvStore } = require('./src/server/config/env-store');
const { createDockerComposeRuntime } = require('./src/server/docker/compose');
const { readRequestBody, writeJson } = require('./src/server/http');
const { createCleanupService } = require('./src/server/services/cleanup');
const { createServiceComposeConfigBuilder } = require('./src/server/services/compose-config');
const { createDeploymentPlanner } = require('./src/server/services/deployment-plan');
const { createLogStreamService } = require('./src/server/services/log-stream');
const { createServiceRegistry } = require('./src/server/services/registry');
const { createServiceRuntime } = require('./src/server/services/runtime');
const CONFIG_META = require('./config-meta');
const args = process.argv.slice(2);
const command = args[0] && !args[0].startsWith('--') ? args[0] : null;
// --- Config ---
const portArg = args.find(arg => arg.startsWith('--port='));
const PORT = portArg ? parseInt(portArg.split('=')[1]) : (process.env.PORT || 3300);
const REQUESTED_APP_ROOT = resolveAppRoot();
const APP_CONTEXT = resolveAppContext(REQUESTED_APP_ROOT);
const APP_ROOT = APP_CONTEXT.appRoot;
const APP_TYPE = APP_CONTEXT.appType;
const APP_DIR = APP_CONTEXT.appDir;
const YAML_CONFIG_PATH = APP_CONTEXT.yamlPath;
const ENV_FILE_PATH = path.join(APP_DIR, '.env');
const HISTORY_DIR = path.join(APP_DIR, '.env_history');
process.env.APP_ROOT = APP_ROOT;
process.env.APP_TYPE = APP_TYPE;
const RUNTIME_DIR = path.join(APP_ROOT, '.config-mate');
if (!fs.existsSync(RUNTIME_DIR)) {
try { fs.mkdirSync(RUNTIME_DIR, { recursive: true }); } catch (e) { }
}
const PID_FILE = path.join(RUNTIME_DIR, `tb-config-mate-${PORT}.pid`);
const LOG_FILE = path.join(RUNTIME_DIR, `tb-config-mate-${PORT}.log`);
const CLEANUP_BACKUP_ROOT = path.join(RUNTIME_DIR, 'backups');
const AUDIT_LOG_FILE = path.join(RUNTIME_DIR, 'audit.log');
const CONFIG_MATE_PASSWORD = process.env.CONFIG_MATE_PASSWORD || '';
const authService = createAuthService({ password: CONFIG_MATE_PASSWORD });
const AUTH_REQUIRED = authService.authRequired;
const {
createSession,
destroySession,
getAuthToken,
getRequestActor,
getSession,
isAuthenticated,
normalizeOperatorName
} = authService;
const serviceRegistry = createServiceRegistry({ appRoot: APP_ROOT, appType: APP_TYPE });
const { getPackageServiceId, getServiceDefinition, listServiceDefinitions } = serviceRegistry;
const dockerRuntime = createDockerComposeRuntime({ appRoot: APP_ROOT });
const serviceRuntime = createServiceRuntime({ docker: dockerRuntime, getServiceDefinition });
const { getServiceStatus, runComposeAction } = serviceRuntime;
const envStore = createEnvStore({
envFilePath: ENV_FILE_PATH,
historyDir: HISTORY_DIR,
configMeta: CONFIG_META,
logger: console
});
const { parseEnvFile, saveEnvFile } = envStore;
const deploymentPlanner = createDeploymentPlanner({
appType: APP_TYPE,
getPackageServiceId,
getServiceDefinition,
getServiceStatus,
runComposeAction,
configProvider: parseEnvFile
});
const {
applyAppConfigChange,
buildDeploymentPlan,
buildDeploymentPlanWithStatus,
checkRequiredDependencies,
dependencyBlockResult,
guardAppServiceDependencies,
guardAppServiceRunning
} = deploymentPlanner;
const cleanupService = createCleanupService({
appRoot: APP_ROOT,
runtimeDir: RUNTIME_DIR,
backupRoot: CLEANUP_BACKUP_ROOT,
auditLogFile: AUDIT_LOG_FILE,
cleanupServiceDataDirs: serviceRegistry.cleanupServiceDataDirs,
getServiceDefinition,
getPackageServiceId,
getServiceStatus,
docker: dockerRuntime
});
const { buildCleanupPlan, runCleanupService } = cleanupService;
const logStreamService = createLogStreamService({
appRoot: APP_ROOT,
docker: dockerRuntime,
getServiceDefinition,
defaultServiceId: getPackageServiceId
});
let serviceComposeConfigBuilder = null;
function buildDiagnosticItem(id, label, ok, detail, severity = 'error') {
return {
id,
label,
state: ok ? 'ok' : severity,
detail
};
}
function buildDeploymentDiagnostics() {
const dockerMessage = dockerRuntime.readyMessage();
const socketMounted = fs.existsSync('/var/run/docker.sock') || os.platform() === 'win32';
const serviceDefs = listServiceDefinitions();
const existingServices = serviceDefs.filter(service => service.exists);
const appDef = getServiceDefinition(getPackageServiceId());
const checks = [
buildDiagnosticItem(
'app-root',
'安装包目录',
fs.existsSync(APP_ROOT),
fs.existsSync(APP_ROOT) ? APP_ROOT : `目录不存在:${APP_ROOT}`
),
buildDiagnosticItem(
'app-env',
'业务配置',
fs.existsSync(ENV_FILE_PATH),
fs.existsSync(ENV_FILE_PATH) ? ENV_FILE_PATH : `未找到 .env:${ENV_FILE_PATH}`
),
buildDiagnosticItem(
'yaml-config',
'YAML 模板',
!!YAML_CONFIG_PATH && fs.existsSync(YAML_CONFIG_PATH),
YAML_CONFIG_PATH && fs.existsSync(YAML_CONFIG_PATH) ? YAML_CONFIG_PATH : '未找到 YAML 模板,首次补全配置可能不完整。',
'warning'
),
buildDiagnosticItem(
'docker-socket',
'Docker Socket',
socketMounted,
socketMounted ? '/var/run/docker.sock 已挂载' : '未挂载 /var/run/docker.sock,无法控制宿主机 Docker。'
),
buildDiagnosticItem(
'docker-compose',
'Docker Compose',
!dockerMessage,
dockerMessage || `${dockerRuntime.dockerComposeCmd || 'docker compose'} 可用`
),
buildDiagnosticItem(
'app-compose',
'业务 Compose',
!!appDef?.exists,
appDef?.exists ? appDef.composePath : `未找到 ${appDef?.composePath || '业务 compose'}`
),
buildDiagnosticItem(
'service-compose',
'服务 Compose',
existingServices.length > 0,
`${existingServices.length}/${serviceDefs.length} 个服务 compose 可用`,
'warning'
),
buildDiagnosticItem(
'auth',
'登录保护',
AUTH_REQUIRED,
AUTH_REQUIRED ? '已启用管理口令' : '未配置 CONFIG_MATE_PASSWORD,高权限控制台未受保护。',
'warning'
)
];
const counts = checks.reduce((acc, check) => {
acc[check.state] = (acc[check.state] || 0) + 1;
return acc;
}, { ok: 0, warning: 0, error: 0 });
return {
status: counts.error > 0 ? 'error' : (counts.warning > 0 ? 'warning' : 'ok'),
counts,
checks
};
}
// --- Helper: Check Status ---
function getRunningPid(pidPath = PID_FILE) {
if (!fs.existsSync(pidPath)) return null;
try {
const pid = parseInt(fs.readFileSync(pidPath, 'utf8'));
process.kill(pid, 0); // Check if process exists
return pid;
} catch (e) {
return null; // Stale PID file or process not running
}
}
// --- Log Rotation (Daemon Mode) ---
function setupLogger() {
const MAX_SIZE = 10 * 1024 * 1024; // 10MB
const MAX_FILES = 5;
// Helper: Rotate files .log -> .log.1 -> .log.2 ...
function rotate() {
try {
if (fs.existsSync(LOG_FILE)) {
try {
const stats = fs.statSync(LOG_FILE);
if (stats.size < MAX_SIZE) return;
} catch (e) { return; } // File might be gone
} else {
return;
}
// Rotate existing backups
for (let i = MAX_FILES - 1; i >= 1; i--) {
const oldF = `${LOG_FILE}.${i}`;
const newF = `${LOG_FILE}.${i + 1}`;
if (fs.existsSync(oldF)) {
try { fs.renameSync(oldF, newF); } catch (e) { }
}
}
// Rotate current
try { fs.renameSync(LOG_FILE, `${LOG_FILE}.1`); } catch (e) { }
} catch (e) {
// Last resort fallback
}
}
let logStream = null;
function openStream() {
// Check rotation before opening
rotate();
logStream = fs.createWriteStream(LOG_FILE, { flags: 'a' });
// Handle stream errors
logStream.on('error', (err) => {
// If stream fails, we can't do much in background, maybe try reopen?
});
}
openStream();
function writeLog(args, level = 'INFO') {
const msg = args.map(a => (typeof a === 'string' ? a : (a instanceof Error ? a.stack : JSON.stringify(a)))).join(' ');
const ts = new Date().toISOString();
const line = `[${ts}] [${level}] ${msg}\n`;
if (logStream) {
// Check size periodically or on write?
// fs.statSync is expensive per log?
// Use bytesWritten relative?
// Simplest robust way: check bytesWritten + periodic stat?
// Let's rely on internal counter and occasional stat?
// Or just check fs.statSync? For a config tool, logs aren't high frequency.
// But let's be safe. Check bytesWritten.
if (logStream.bytesWritten > MAX_SIZE) {
logStream.end();
openStream();
}
logStream.write(line);
}
}
// Override console
console.log = (...args) => writeLog(args, 'INFO');
console.error = (...args) => writeLog(args, 'ERROR');
console.warn = (...args) => writeLog(args, 'WARN');
console.debug = (...args) => writeLog(args, 'DEBUG');
// Also catch unhandled exceptions to log file
process.on('uncaughtException', (err) => {
writeLog([err], 'FATAL');
process.exit(1);
});
}
// Enable rotation if running as daemon
if (args.includes('--daemon')) {
setupLogger();
}
// --- CLI Commands ---
// 处理版本号查询
if (args.includes('-v') || args.includes('--version')) {
const packageJson = require('./package.json');
console.log(`ThingsBoard Config Mate v${packageJson.version}`);
process.exit(0);
}
if (args.includes('-h') || args.includes('--help')) {
const packageJson = require('./package.json');
console.log(`
ThingsBoard Config Mate (TB-CM) v${packageJson.version} - 命令行使用指南
用法:
tb-config-mate [命令] [选项]
命令:
start 在后台启动配置服务 (默认端口 3300)
stop 停止正在运行的后台服务 (需指定端口,默认停止 3300)
restart 停止并重新启动后台服务
status 查看后台服务的运行状态 (未指定端口时显示所有实例)
选项:
--port=N 指定服务运行的端口 (默认: 3300)
-v, --version 显示版本号
-h, --help 显示此帮助信息
示例:
使用指定端口启动:
./tb-config-mate start --port=4005
查看状态:
./tb-config-mate status
./tb-config-mate status --port=4005
`);
process.exit(0);
}
if (command === 'status') {
if (portArg) {
// Check specific port
const pid = getRunningPid();
if (pid) {
console.log(`[Status] Service (Port ${PORT}) is RUNNING (PID: ${pid})`);
} else {
console.log(`[Status] Service (Port ${PORT}) is STOPPED`);
}
} else {
// Scan all pid files
const files = fs.readdirSync(RUNTIME_DIR).filter(f => f.startsWith('tb-config-mate-') && f.endsWith('.pid'));
if (files.length === 0) {
console.log('[Status] No running instances found.');
} else {
console.log('[Status] Found instances:');
files.forEach(f => {
const portMatch = f.match(/tb-config-mate-(\d+)\.pid/);
const p = portMatch ? portMatch[1] : 'Unknown';
const pid = getRunningPid(path.join(RUNTIME_DIR, f));
if (pid) {
console.log(` - Port ${p}: RUNNING (PID: ${pid})`);
} else {
console.log(` - Port ${p}: STOPPED (Stale PID file)`);
}
});
}
}
process.exit(0);
}
if (command === 'stop') {
const pid = getRunningPid();
if (pid) {
try {
process.kill(pid);
console.log(`[Success] Stopped service on Port ${PORT} (PID: ${pid})`);
if (fs.existsSync(PID_FILE)) fs.unlinkSync(PID_FILE);
} catch (e) {
console.error(`[Error] Failed to stop: ${e.message}`);
}
} else {
console.log(`[Info] Service on Port ${PORT} is not running.`);
}
process.exit(0);
}
if (command === 'restart') {
const pid = getRunningPid();
if (pid) {
try {
process.kill(pid);
console.log(`[Success] Stopped service on Port ${PORT} (PID: ${pid})`);
if (fs.existsSync(PID_FILE)) fs.unlinkSync(PID_FILE);
} catch (e) {
console.warn(`[Warn] Failed to stop previous instance: ${e.message}`);
}
}
}
if (command === 'start' || command === 'restart') {
if (command === 'start' && getRunningPid()) {
console.log(`[Info] Service is already running (PID: ${getRunningPid()}).`);
process.exit(0);
}
console.log(`[Info] Starting background service...`);
const logFd = fs.openSync(LOG_FILE, 'a');
const childArgs = args.filter(a => !['start', 'stop', 'restart', 'status'].includes(a));
// Always pass the entry script path (__filename) to the child process, unless running in pkg.
// In 'pkg', __filename resolves to the internal snapshot path (e.g. /snapshot/.../tb-config-src.js).
// Using process.pkg to detect if we're in a packaged binary.
const spawnCmd = process.pkg ? process.execPath : process.execPath;
let spawnArgs = process.pkg ? [...childArgs, '--daemon'] : [__filename, ...childArgs, '--daemon'];
const child = spawn(spawnCmd, spawnArgs, {
detached: true,
stdio: 'ignore', // Let the child manage its own logging via --daemon
cwd: process.cwd(),
env: process.env
});
child.unref();
console.log(`[Success] Started (PID: ${child.pid})`);
console.log(`[Log] > ${LOG_FILE}`);
process.exit(0);
}
// --- Overwrite Mode (--over) ---
if (args.includes('--over')) {
console.log('[Info] Mode: Configuration Overwrite');
if (!fs.existsSync(ENV_FILE_PATH)) {
console.error('[Error] .env file not found.');
process.exit(1);
}
// 1. Parse .env
const envVars = {};
const envContent = fs.readFileSync(ENV_FILE_PATH, 'utf-8');
envContent.split('\n').forEach(line => {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('#')) {
const parts = trimmed.split('=');
const key = parts[0].trim();
// Value: join remaining parts in case value contains '='
const val = parts.slice(1).join('=').trim();
envVars[key] = val;
}
});
const totalEnvKeys = Object.keys(envVars).length;
console.log(`[Info] Loaded ${totalEnvKeys} variables from .env`);
// 2. Identify Target File
const appType = envVars['APPTYPE'] || 'CLOUD';
let targetFile = null;
if (appType === 'EDGE') {
const candidates = [
path.join(APP_DIR, 'conf', 'tb-edge.yml'),
path.join(APP_DIR, 'tb-edge.yml'),
YAML_CONFIG_PATH
];
targetFile = candidates.find(f => fs.existsSync(f));
} else {
const candidates = [
path.join(APP_DIR, 'conf', 'thingsboard.yml'),
path.join(APP_DIR, 'thingsboard.yml'),
YAML_CONFIG_PATH
];
targetFile = candidates.find(f => fs.existsSync(f));
}
if (!targetFile) {
console.error(`[Error] Target configuration file for ${appType} not found.`);
process.exit(1);
}
console.log(`[Info] Target Config: ${targetFile}`);
// 3. Backup
const timestamp = new Date().toISOString().replace(/[-:T]/g, '').slice(0, 14); // YYYYMMDDHHmmss
const backupFile = `${targetFile}.${timestamp}.bak`;
try {
fs.copyFileSync(targetFile, backupFile);
console.log(`[Backup] Created: ${path.basename(backupFile)}`);
} catch (e) {
console.error('[Error] Backup failed:', e);
process.exit(1);
}
// 4. Regex Scanning & Replacement
let fileContent = fs.readFileSync(targetFile, 'utf8');
let updateCount = 0;
let unchangedCount = 0;
console.log('[Process] Starting placeholder replacement...');
console.log('----------------------------------------------------------------');
Object.keys(envVars).forEach(key => {
const newValue = envVars[key];
// Match ${KEY} or ${KEY:default}
// Regex Explanation:
// \$\{ Literal ${
// KEY Variable Name
// (:[^}]*)? Optional group: colon followed by anything non-} (the default value)
// \} Literal }
const regex = new RegExp(`\\$\\{${key}(:[^}]*)?\\}`, 'g');
let hasMatch = false;
// We use a callback to perform replacement to allow for logic (escaping, logging) values
fileContent = fileContent.replace(regex, (match, defaultGroup, offset, string) => {
hasMatch = true;
// Safety Check: Is this placeholder inside double quotes?
// Simple heuristic: look at chars immediately before/after match
const prevChar = string[offset - 1];
const nextChar = string[offset + match.length];
const isQuoted = (prevChar === '"' && nextChar === '"');
let finalValue = newValue;
// If inside quotes, escape quotes in the value
if (isQuoted) {
finalValue = finalValue.replace(/"/g, '\\"');
}
// Construct replacement: ${KEY:NEW_VALUE}
const replacement = `\${${key}:${finalValue}}`;
if (match !== replacement) {
console.log(`[Updated] ${key}`);
console.log(` Before: "${match}"`);
console.log(` After: "${replacement}"`);
console.log('----------------------------------------------------------------');
updateCount++;
return replacement;
} else {
return match; // No change
}
});
if (!hasMatch) {
// Log skip only if verbose? Or maybe specific ones?
// User asked for "Detailed log", but logging every skip for 100 vars might be too much.
// Let's print matched ones prominently.
unchangedCount++;
}
});
try {
fs.writeFileSync(targetFile, fileContent, 'utf8');
console.log(`[Success] Overwrite complete.`);
console.log(` - Total Env Vars: ${totalEnvKeys}`);
console.log(` - Replacements: ${updateCount}`);
console.log(` - Unchanged/Skip: ${unchangedCount}`);
} catch (e) {
console.error('[Error] Failed to write changes:', e);
// Restore backup? maybe manual
}
process.exit(0);
} else {
// Regular Server Startup continue...
}
// const { getHtml } = require('./ui-template'); // Removed
// Try to lazy load js-yaml to avoid hard crash if missing (though we installed it)
let yaml;
try {
yaml = require('js-yaml');
} catch (e) {
console.warn('[Warn] js-yaml not found. Auto-init from YAML disabled.');
}
serviceComposeConfigBuilder = createServiceComposeConfigBuilder({
yaml,
getServiceDefinition,
getPackageServiceId,
envProvider: parseEnvFile
});
const { buildServiceComposeConfig } = serviceComposeConfigBuilder;
// --- Auto-Init Logic ---
function tryInitFromYaml() {
let existingEnv = {};
if (fs.existsSync(ENV_FILE_PATH)) {
try {
existingEnv = parseEnvFile();
const keyCount = Object.keys(existingEnv).length;
console.log(`[Info] .env file exists with ${keyCount} keys. Checking for missing configurations...`);
} catch (e) {
console.warn('[Warn] Failed to parse existing .env, will create new:', e);
}
} else {
console.log('[Info] .env file not found. Will create from YAML...');
}
if (!yaml) return;
console.log('[Info] Looking for YAML config...');
const candidates = [
YAML_CONFIG_PATH,
path.join(APP_DIR, 'conf', 'thingsboard.yml'),
path.join(APP_DIR, 'conf', 'tb-edge.yml'),
path.join(APP_DIR, 'thingsboard.yml'),
path.join(APP_DIR, 'tb-edge.yml'),
path.join(APP_ROOT, 'conf', 'thingsboard.yml'),
path.join(APP_ROOT, 'conf', 'tb-edge.yml'),
path.join(__dirname, 'conf', 'thingsboard.yml'),
path.join(__dirname, 'conf', 'tb-edge.yml')
].filter(Boolean);
let yamlPath = null;
for (const p of candidates) {
if (fs.existsSync(p)) {
yamlPath = p;
break;
}
}
if (!yamlPath) {
console.log('[Info] No YAML config found in conf/ directory. Skipping.');
return;
}
console.log(`[Info] Found YAML config at: ${yamlPath}`);
try {
const fileContents = fs.readFileSync(yamlPath, 'utf8');
const data = yaml.load(fileContents);
const flattened = flattenYaml(data);
// Auto-extract Env Vars from values
Object.keys(flattened).forEach(flatKey => {
const val = flattened[flatKey];
if (typeof val === 'string') {
const match = val.match(/^\$\{([^:]+)(:.*)?\}$/);
if (match) {
const varName = match[1];
if (!flattened[varName]) {
flattened[varName] = val;
}
}
}
});
const YAML_KEY_MAPPING = {
"REDIS_CONNECTION_TYPE": ["REDIS_CONNECTION_TYPE"],
"REDIS_HOST": ["REDIS_STANDALONE_HOST"],
"REDIS_PORT": ["REDIS_STANDALONE_PORT"],
"REDIS_USE_DEFAULT_CLIENT_CONFIG": ["REDIS_STANDALONE_USEDEFAULTCLIENTCONFIG"],
"REDIS_CLIENT_NAME": ["REDIS_STANDALONE_CLIENTNAME"],
"REDIS_CLIENT_CONNECT_TIMEOUT": ["REDIS_STANDALONE_CONNECTTIMEOUT"],
"REDIS_CLIENT_READ_TIMEOUT": ["REDIS_STANDALONE_READTIMEOUT"],
"REDIS_CLIENT_USE_POOL_CONFIG": ["REDIS_STANDALONE_USEPOOLCONFIG"],
"REDIS_NODES": ["REDIS_CLUSTER_NODES"],
"REDIS_MAX_REDIRECTS": ["REDIS_CLUSTER_MAX_REDIRECTS"],
"REDIS_USE_DEFAULT_POOL_CONFIG": ["REDIS_CLUSTER_USEDEFAULTPOOLCONFIG", "REDIS_USE_DEFAULT_POOL_CONFIG"],
"REDIS_MASTER": ["REDIS_SENTINEL_MASTER"],
"REDIS_SENTINELS": ["REDIS_SENTINEL_SENTINELS"],
"REDIS_SENTINEL_PASSWORD": ["REDIS_SENTINEL_PASSWORD"],
"REDIS_SENTINEL_USE_DEFAULT_POOL_CONFIG": ["REDIS_SENTINEL_USEDEFAULTPOOLCONFIG"],
"SPRING_DRIVER_CLASS_NAME": ["SPRING_DATASOURCE_DRIVERCLASSNAME"],
"NETTY_MAX_PAYLOAD_SIZE": ["TRANSPORT_MQTT_NETTY_MAX_PAYLOAD_SIZE"],
"MQTT_BIND_PORT": ["TRANSPORT_MQTT_BIND_PORT"]
};
const newConfig = {};
// Infer APPTYPE based on filename
let targetAppType = 'CLOUD';
const filename = path.basename(yamlPath);
if (filename === 'thingsboard.yml') {
newConfig['APPTYPE'] = 'CLOUD';
targetAppType = 'CLOUD';
} else if (filename === 'tb-edge.yml') {
newConfig['APPTYPE'] = 'EDGE';
targetAppType = 'EDGE';
}
// Build Reverse Mapping from YAML values
// Scan all flattened values. If a value contains "${KEY:DEFAULT}" or "${KEY}",
// we map KEY -> value (the placeholder string itself).
// This allows automatic discovery of keys without manual mapping.
const reverseMapping = {};
Object.keys(flattened).forEach(flatKey => {
const val = flattened[flatKey];
if (typeof val === 'string') {
// Regex to match ${KEY} or ${KEY:DEFAULT}
// We capture the KEY name.
// Note: YAML might have nested structure like "${HOST}:${PORT}", so we iterate all matches.
// But for simple config extraction, usually one key per value.
const regex = /\$\{([A-Z0-9_]+)(?::[^}]*)?\}/g;
let match;
while ((match = regex.exec(val)) !== null) {
const envKey = match[1];
// If we have multiple occurrences (rare), last one wins or we ignore collisions.
// We store the full original value string which resolveSpringPlaceholder can handle.
// But resolveSpringPlaceholder expects the *entire* string to be the value to parse.
reverseMapping[envKey] = val;
}
}
});
Object.keys(CONFIG_META).forEach(metaKey => {
const meta = CONFIG_META[metaKey];
const scope = meta.scope || 'common';
if (scope === 'cloud' && targetAppType !== 'CLOUD') return;
if (scope === 'edge' && targetAppType !== 'EDGE') return;
// 注意: 不再检查 dependsOn,所有配置项都会被提取到 .env
// UI 显示/隐藏由前端的 dependsOn 逻辑控制
// Priority 1: Direct key match (doubtful for YAML but possible)
if (flattened[metaKey] !== undefined) {
newConfig[metaKey] = resolveSpringPlaceholder(flattened[metaKey]);
return;
}
// Priority 2: Auto-discovered Reverse Mapping (The Magic Fix)
if (reverseMapping[metaKey] !== undefined) {
newConfig[metaKey] = resolveSpringPlaceholder(reverseMapping[metaKey]);
return;
}
// Priority 3: Explicit Manual Mapping (Legacy/Fallback)
if (YAML_KEY_MAPPING[metaKey]) {
const mappedKeys = YAML_KEY_MAPPING[metaKey];
for (const mappedKey of mappedKeys) {
if (flattened[mappedKey] !== undefined) {
newConfig[metaKey] = resolveSpringPlaceholder(flattened[mappedKey]);
return;
}
}
}
// Priority 4: Special Handling for Legacy Edge Keys (No Env Var in YAML)
if (targetAppType === 'EDGE') {
if (metaKey === 'CLOUD_CHECK_STATUS_BASE_URL' && data?.cloud?.check_status?.baseURL) {
newConfig[metaKey] = data.cloud.check_status.baseURL;
return;
}
if (metaKey === 'EDGES_STORAGE_HISTORY_STATUS' && data?.cloud?.rpc?.storage?.history_status !== undefined) {
newConfig[metaKey] = String(data.cloud.rpc.storage.history_status);
return;
}
if (metaKey === 'TELEMETRY_SEPARATION_ENABLED' && data?.cloud?.telemetry?.separation?.enabled !== undefined) {
newConfig[metaKey] = String(data.cloud.telemetry.separation.enabled);
return;
}
}
});
if (Object.keys(newConfig).length > 0) {
console.log(`[Info] Extracted ${Object.keys(newConfig).length} configurations from YAML.`);
// Calculate missing keys (present in newConfig but NOT in existingEnv)
const missingKeys = {};
let missingCount = 0;
Object.keys(newConfig).forEach(key => {
// Use Object.prototype.hasOwnProperty for safety
if (!Object.prototype.hasOwnProperty.call(existingEnv, key)) {
missingKeys[key] = newConfig[key];
missingCount++;
}
});
// Log missing keys details
const expectedKeys = [];
Object.keys(CONFIG_META).forEach(k => {
const meta = CONFIG_META[k];
const scope = meta.scope || 'common';
if (scope === 'cloud' && targetAppType !== 'CLOUD') return;
if (scope === 'edge' && targetAppType !== 'EDGE') return;
// 不再根据 dependsOn 跳过,所有配置项都应该被提取
expectedKeys.push(k);
});
const missedExtraction = expectedKeys.filter(k => !newConfig[k] && !existingEnv[k]);
if (missedExtraction.length > 0) {
console.log(`[Info] ⚠️ The following ${missedExtraction.length} keys were expected but NOT found in YAML or .env. Using defaults:`);
missedExtraction.forEach(k => {
console.log(` - ${k}`);
const meta = CONFIG_META[k];
// Priority 5: Default value from Metadata (Fallback)
if (meta.default !== undefined) {
missingKeys[k] = meta.default;
missingCount++;
} else {
// If no default, maybe set empty? Or skip?
// User request implies "extract all defined here to env".
// Let's set it to empty string if no default, to ensure it exists.
missingKeys[k] = '';
missingCount++;
}
});
}
if (missingCount > 0) {
console.log(`[Info] Found ${missingCount} missing keys. Updating .env...`);
if (Object.keys(existingEnv).length === 0 && !fs.existsSync(ENV_FILE_PATH)) {
// New file: Create clean
saveEnvFile(missingKeys);
} else {
// Existing file: Append only
let appendContent = '\n# --- Auto-Generated Defaults ---\n';
Object.keys(missingKeys).sort().forEach(key => {
const meta = CONFIG_META[key];
if (meta) {
if (meta.comment) {
appendContent += `# ${meta.label} (${meta.comment})\n`;
} else {
appendContent += `# ${meta.label}\n`;
}
}
appendContent += `${key}=${missingKeys[key]}\n`;
});
try {
fs.appendFileSync(ENV_FILE_PATH, appendContent);
console.log('[Success] Appended missing configurations to .env');
} catch (err) {
console.error('[Error] Failed to append to .env:', err);
}
}
} else {
console.log('[Info] .env is already complete. No new keys to add.');
}
} else {
console.log('[Warn] Parsed YAML but found no matching configurations defined in metadata.');
}
} catch (e) {
console.error('[Error] Failed to parse YAML:', e);
}
}
function flattenYaml(obj, prefix = '', res = {}) {
for (const key in obj) {
if (!obj.hasOwnProperty(key)) continue;
const val = obj[key];
// Convert camelCase or snake_case key to UPPER_UNDERSCORE for standard ENV
// But wait, thingsboard.yml keys are usually snake_case or camelCase?
// Actually usually standard YAML keys are lowercase/mixed.
// e.g. spring: datasource: url
// We want SPRING_DATASOURCE_URL.
// Normalize key to uppercase
const upperKey = key.toUpperCase();
const newKey = prefix ? `${prefix}_${upperKey}` : upperKey;
if (typeof val === 'object' && val !== null && !Array.isArray(val)) {
flattenYaml(val, newKey, res);
} else {
res[newKey] = String(val);
}
}
return res;
}
function resolveSpringPlaceholder(val) {
if (typeof val !== 'string') return val;
val = val.trim();
// Match ${VAR:default} or ${VAR:} (empty default)
// Note: This regex assumes simple nesting or no nesting.
// Captures: 1=VAR, 2=default (can be empty)
const match = val.match(/^\$\{([^:]+):(.*)\}$/);
if (match) {
return match[2]; // Return default value (can be empty string)
}
// Match ${VAR} (no default) -> return empty string or keep it?
// If it's a variable without default, likely meant to be set by env.
// Returning empty string for .env initialization seems safer than leaving raw ${VAR}.
const matchNoDefault = val.match(/^\$\{([^:]+)\}$/);
if (matchNoDefault) {
return "";
}
return val;
}
// --- HTTP Server ---
function serveStaticAsset(pathname, res, headers) {
const assetRoot = path.resolve(__dirname, 'assets');
const relativePath = decodeURIComponent(pathname.replace(/^\/assets\//, ''));
const assetPath = path.resolve(assetRoot, relativePath);
if (!assetPath.startsWith(assetRoot + path.sep)) {
writeJson(res, 403, { status: 'error', message: 'Forbidden' }, headers);
return;
}
if (!fs.existsSync(assetPath) || !fs.statSync(assetPath).isFile()) {
writeJson(res, 404, { status: 'error', message: 'Asset not found' }, headers);
return;
}
const ext = path.extname(assetPath).toLowerCase();
const contentTypes = {
'.css': 'text/css; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.webp': 'image/webp'
};
res.writeHead(200, {
...headers,
'Content-Type': contentTypes[ext] || 'application/octet-stream',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.end(fs.readFileSync(assetPath));
}
function startServer() {
const server = http.createServer((req, res) => {
const { method } = req;
const requestUrl = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
const url = req.url;
const pathname = requestUrl.pathname;
const headers = {
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
};
if (method === 'OPTIONS') {
res.writeHead(204, headers);
res.end();
return;
}
if (pathname === '/' || pathname === '/index.html') {
const htmlPath = path.join(__dirname, 'index.html');
console.log(`[Debug] Loading HTML from: ${htmlPath}`);
const html = fs.readFileSync(htmlPath, 'utf-8');
res.writeHead(200, {
...headers,
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'
});
res.end(html);
return;
}
if (pathname.startsWith('/assets/') && method === 'GET') {
serveStaticAsset(pathname, res, headers);
return;
}
if (pathname === '/api/health' && method === 'GET') {
writeJson(res, 200, {
status: 'ok',
appRoot: APP_ROOT,
appDir: APP_DIR,
appType: APP_TYPE,
docker: {
available: !dockerRuntime.readyMessage(),
message: dockerRuntime.readyMessage()
}
}, headers);
return;
}
if (pathname === '/api/auth/status' && method === 'GET') {
const session = getSession(req);
writeJson(res, 200, {
required: AUTH_REQUIRED,
authenticated: isAuthenticated(req),
operator: session?.operator || ''
}, headers);
return;
}
if (pathname === '/api/login' && method === 'POST') {
readRequestBody(req).then(body => {
try {
const payload = JSON.parse(body || '{}');
const operator = normalizeOperatorName(payload.operator);
if (!operator) {
writeJson(res, 400, { status: 'error', message: '请输入操作员名称' }, headers);
return;
}
if (!AUTH_REQUIRED || payload.password === CONFIG_MATE_PASSWORD) {
const token = createSession(req, operator);
writeJson(res, 200, { status: 'success', operator: normalizeOperatorName(operator) || 'operator' }, {
...headers,
'Set-Cookie': `config_mate_session=${encodeURIComponent(token)}; HttpOnly; SameSite=Lax; Path=/; Max-Age=86400`
});
} else {
writeJson(res, 401, { status: 'error', message: '密码错误' }, headers);
}
} catch (e) {
writeJson(res, 400, { status: 'error', message: e.message }, headers);
}
});
return;
}
if (pathname === '/api/logout' && method === 'POST') {
const token = getAuthToken(req);