bs9 1.1.0 → 1.3.1

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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # BS9 (Bun Sentinel 9) 🚀
2
2
 
3
3
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
- [![Version](https://img.shields.io/badge/version-1.0.0-blue.svg)](https://github.com/bs9/bs9)
4
+ [![Version](https://img.shields.io/badge/version-1.3.0-blue.svg)](https://github.com/bs9/bs9)
5
5
  [![Security](https://img.shields.io/badge/security-Enterprise-green.svg)](SECURITY.md)
6
6
  [![Production Ready](https://img.shields.io/badge/production-Ready-brightgreen.svg)](PRODUCTION.md)
7
7
  [![Cross-Platform](https://img.shields.io/badge/platform-Linux%20%7C%20macOS%20%7C%20Windows-lightgrey.svg)](https://github.com/bs9/bs9)
@@ -81,6 +81,13 @@ bs9 monit --refresh 5 # Custom refresh interval
81
81
  bs9 web --port 8080 # Start web dashboard
82
82
  bs9 web --detach --port 8080 # Run in background
83
83
 
84
+ # Advanced monitoring
85
+ bs9 advanced --port 8090
86
+
87
+ # Performance profiling
88
+ bs9 profile --service myapp --duration 60
89
+ bs9 profile --service myapp --duration 60 --output profile.json
90
+
84
91
  # Alert management
85
92
  bs9 alert --list # Show alert configuration
86
93
  bs9 alert --cpu 80 --memory 85 # Set thresholds
@@ -89,6 +96,7 @@ bs9 alert --test # Test webhook
89
96
 
90
97
  # Historical data
91
98
  bs9 export --format json --hours 24 # Export metrics
99
+ bs9 export --service myapp --format csv --hours 24
92
100
  bs9 export --service myapp --format csv
93
101
  ```
94
102
 
package/bin/bs9 CHANGED
@@ -15,7 +15,10 @@ import { profileCommand } from "../src/commands/profile.js";
15
15
  import { loadbalancerCommand } from "../src/loadbalancer/manager.js";
16
16
  import { windowsCommand } from "../src/windows/service.js";
17
17
  import { launchdCommand } from "../src/macos/launchd.js";
18
+ import { updateCommand } from "../src/commands/update.js";
18
19
  import { dbpoolCommand } from "../src/database/pool.js";
20
+ import { advancedMonitoringCommand } from "../src/monitoring/advanced.js";
21
+ import { consulCommand } from "../src/discovery/consul.js";
19
22
 
20
23
  const program = new Command();
21
24
 
@@ -145,4 +148,34 @@ program
145
148
  .option("--iterations <num>", "Test iterations", "100")
146
149
  .action(dbpoolCommand);
147
150
 
151
+ program
152
+ .command("update")
153
+ .description("Update BS9 to latest version")
154
+ .option("--check", "Check for updates without installing")
155
+ .option("--force", "Force update even if already latest")
156
+ .option("--rollback", "Rollback to previous version")
157
+ .option("--version <version>", "Update to specific version")
158
+ .action(updateCommand);
159
+
160
+ program
161
+ .command("advanced")
162
+ .description("Advanced monitoring dashboard")
163
+ .option("--port <port>", "Dashboard port", "8090")
164
+ .action(advancedMonitoringCommand);
165
+
166
+ program
167
+ .command("consul")
168
+ .description("Consul service discovery")
169
+ .argument("<action>", "Action to perform")
170
+ .option("--consul-url <url>", "Consul URL", "http://localhost:8500")
171
+ .option("--name <name>", "Service name")
172
+ .option("--id <id>", "Service ID")
173
+ .option("--address <address>", "Service address")
174
+ .option("--port <port>", "Service port")
175
+ .option("--tags <tags>", "Service tags (comma-separated)")
176
+ .option("--health-check <url>", "Health check URL")
177
+ .option("--meta <json>", "Service metadata (JSON)")
178
+ .option("--service <service>", "Service name for discovery")
179
+ .action(consulCommand);
180
+
148
181
  program.parse();
@@ -0,0 +1,181 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { Command } from "commander";
4
+ import { startCommand } from "../src/commands/start.js";
5
+ import { stopCommand } from "../src/commands/stop.js";
6
+ import { restartCommand } from "../src/commands/restart.js";
7
+ import { statusCommand } from "../src/commands/status.js";
8
+ import { logsCommand } from "../src/commands/logs.js";
9
+ import { monitCommand } from "../src/commands/monit.js";
10
+ import { webCommand } from "../src/commands/web.js";
11
+ import { alertCommand } from "../src/commands/alert.js";
12
+ import { exportCommand } from "../src/commands/export.js";
13
+ import { depsCommand } from "../src/commands/deps.js";
14
+ import { profileCommand } from "../src/commands/profile.js";
15
+ import { loadbalancerCommand } from "../src/loadbalancer/manager.js";
16
+ import { windowsCommand } from "../src/windows/service.js";
17
+ import { launchdCommand } from "../src/macos/launchd.js";
18
+ import { updateCommand } from "../src/commands/update.js";
19
+ import { dbpoolCommand } from "../src/database/pool.js";
20
+ import { advancedMonitoringCommand } from "../src/monitoring/advanced.js";
21
+ import { consulCommand } from "../src/discovery/consul.js";
22
+
23
+ const program = new Command();
24
+
25
+ program
26
+ .name("bs9")
27
+ .description("BS9 (Bun Sentinel 9) — Mission-critical process manager CLI")
28
+ .version("1.0.0");
29
+
30
+ program
31
+ .command("start")
32
+ .description("Start a process with hardened systemd unit")
33
+ .argument("<file>", "Application file to start")
34
+ .option("-n, --name <name>", "Service name")
35
+ .option("-p, --port <port>", "Port number", "3000")
36
+ .option("-h, --host <host>", "Host address", "localhost")
37
+ .option("--https", "Use HTTPS protocol")
38
+ .option("-e, --env <env>", "Environment variables (can be used multiple times)", (value, previous) => [...(previous || []), value])
39
+ .option("--otel", "Enable OpenTelemetry instrumentation", true)
40
+ .option("--prometheus", "Enable Prometheus metrics", true)
41
+ .option("--build", "Build TypeScript to JavaScript before starting")
42
+ .action(startCommand);
43
+
44
+ program
45
+ .command("stop")
46
+ .description("Stop a managed service")
47
+ .argument("<name>", "Service name")
48
+ .action(stopCommand);
49
+
50
+ program
51
+ .command("restart")
52
+ .description("Restart a managed service")
53
+ .argument("<name>", "Service name")
54
+ .action(restartCommand);
55
+
56
+ program
57
+ .command("status")
58
+ .description("Show status and SRE metrics for all services")
59
+ .option("-w, --watch", "Watch mode (refresh every 2s)")
60
+ .action(statusCommand);
61
+
62
+ program
63
+ .command("logs")
64
+ .description("Show logs for a service (via journalctl)")
65
+ .argument("<name>", "Service name")
66
+ .option("-f, --follow", "Follow logs")
67
+ .option("-n, --lines <number>", "Number of lines", "50")
68
+ .action(logsCommand);
69
+
70
+ program
71
+ .command("monit")
72
+ .description("Real-time terminal dashboard for all services")
73
+ .option("-r, --refresh <seconds>", "Refresh interval in seconds", "2")
74
+ .action(monitCommand);
75
+
76
+ program
77
+ .command("web")
78
+ .description("Start web-based monitoring dashboard")
79
+ .option("-p, --port <port>", "Port for web dashboard", "8080")
80
+ .option("-d, --detach", "Run in background")
81
+ .action(webCommand);
82
+
83
+ program
84
+ .command("alert")
85
+ .description("Configure alert thresholds and webhooks")
86
+ .option("--enable", "Enable alerts")
87
+ .option("--disable", "Disable alerts")
88
+ .option("--webhook <url>", "Set webhook URL for alerts")
89
+ .option("--cpu <percentage>", "CPU threshold percentage")
90
+ .option("--memory <percentage>", "Memory threshold percentage")
91
+ .option("--errorRate <percentage>", "Error rate threshold percentage")
92
+ .option("--uptime <percentage>", "Uptime threshold percentage")
93
+ .option("--cooldown <seconds>", "Alert cooldown period in seconds")
94
+ .option("--service <name>", "Configure alerts for specific service")
95
+ .option("--list", "List current alert configuration")
96
+ .option("--test", "Test webhook connectivity")
97
+ .action(alertCommand);
98
+
99
+ program
100
+ .command("export")
101
+ .description("Export historical metrics data")
102
+ .option("-f, --format <format>", "Export format (json|csv)", "json")
103
+ .option("-h, --hours <hours>", "Hours of data to export", "24")
104
+ .option("-o, --output <file>", "Output file path")
105
+ .option("-s, --service <name>", "Export specific service metrics")
106
+ .action(exportCommand);
107
+
108
+ program
109
+ .command("deps")
110
+ .description("Visualize service dependencies")
111
+ .option("-f, --format <format>", "Output format (text|dot|json)", "text")
112
+ .option("-o, --output <file>", "Output file path")
113
+ .action(depsCommand);
114
+
115
+ program
116
+ .command("profile")
117
+ .description("Performance profiling for services")
118
+ .option("-d, --duration <seconds>", "Profiling duration", "60")
119
+ .option("-i, --interval <ms>", "Sampling interval", "1000")
120
+ .option("-s, --service <name>", "Service name to profile")
121
+ .option("-o, --output <file>", "Output file path")
122
+ .action(profileCommand);
123
+
124
+ program
125
+ .command("loadbalancer")
126
+ .description("Load balancer management")
127
+ .argument("<action>", "Action to perform")
128
+ .option("-p, --port <port>", "Load balancer port", "8080")
129
+ .option("-a, --algorithm <type>", "Load balancing algorithm", "round-robin")
130
+ .option("-b, --backends <list>", "Backend servers (host:port,host:port)")
131
+ .option("--health-check", "Enable health checking", true)
132
+ .option("--health-path <path>", "Health check path", "/healthz")
133
+ .option("--health-interval <ms>", "Health check interval", "10000")
134
+ .action(loadbalancerCommand);
135
+
136
+ program
137
+ .command("dbpool")
138
+ .description("Database connection pool management")
139
+ .argument("<action>", "Action to perform")
140
+ .option("--host <host>", "Database host", "localhost")
141
+ .option("--port <port>", "Database port", "5432")
142
+ .option("--database <name>", "Database name", "testdb")
143
+ .option("--username <user>", "Database username", "user")
144
+ .option("--password <pass>", "Database password", "password")
145
+ .option("--max-connections <num>", "Maximum connections", "10")
146
+ .option("--min-connections <num>", "Minimum connections", "2")
147
+ .option("--concurrency <num>", "Test concurrency", "10")
148
+ .option("--iterations <num>", "Test iterations", "100")
149
+ .action(dbpoolCommand);
150
+
151
+ program
152
+ .command("update")
153
+ .description("Update BS9 to latest version")
154
+ .option("--check", "Check for updates without installing")
155
+ .option("--force", "Force update even if already latest")
156
+ .option("--rollback", "Rollback to previous version")
157
+ .option("--version <version>", "Update to specific version")
158
+ .action(updateCommand);
159
+
160
+ program
161
+ .command("advanced")
162
+ .description("Advanced monitoring dashboard")
163
+ .option("--port <port>", "Dashboard port", "8090")
164
+ .action(advancedMonitoringCommand);
165
+
166
+ program
167
+ .command("consul")
168
+ .description("Consul service discovery")
169
+ .argument("<action>", "Action to perform")
170
+ .option("--consul-url <url>", "Consul URL", "http://localhost:8500")
171
+ .option("--name <name>", "Service name")
172
+ .option("--id <id>", "Service ID")
173
+ .option("--address <address>", "Service address")
174
+ .option("--port <port>", "Service port")
175
+ .option("--tags <tags>", "Service tags (comma-separated)")
176
+ .option("--health-check <url>", "Health check URL")
177
+ .option("--meta <json>", "Service metadata (JSON)")
178
+ .option("--service <service>", "Service name for discovery")
179
+ .action(consulCommand);
180
+
181
+ program.parse();
package/dist/bs9.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // @bun
2
- var e="./bs9-064xs9r9.";export{e as default};
2
+ var e="./bs9-33vcpmb9.";export{e as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bs9",
3
- "version": "1.1.0",
3
+ "version": "1.3.1",
4
4
  "description": "Bun Sentinel 9 - High-performance, non-root process manager for Bun",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -29,7 +29,7 @@
29
29
  "author": "xarhang",
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
- "@opentelemetry/api": "^1.9.0",
32
+ "@opentelemetry/api": "^1.8.0",
33
33
  "@opentelemetry/auto-instrumentations-node": "^0.50.0",
34
34
  "@opentelemetry/exporter-trace-otlp-http": "^0.50.0",
35
35
  "@opentelemetry/resources": "^1.23.0",
@@ -0,0 +1,322 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { execSync } from "node:child_process";
4
+ import { existsSync, writeFileSync, mkdirSync } from "node:fs";
5
+ import { join, dirname } from "node:path";
6
+ import { homedir } from "node:os";
7
+ import { getPlatformInfo } from "../platform/detect.js";
8
+
9
+ interface UpdateOptions {
10
+ check?: boolean;
11
+ force?: boolean;
12
+ rollback?: boolean;
13
+ version?: string;
14
+ }
15
+
16
+ interface UpdateInfo {
17
+ currentVersion: string;
18
+ latestVersion: string;
19
+ hasUpdate: boolean;
20
+ releaseNotes?: string;
21
+ downloadUrl?: string;
22
+ }
23
+
24
+ interface BackupInfo {
25
+ version: string;
26
+ timestamp: number;
27
+ files: string[];
28
+ }
29
+
30
+ class BS9Updater {
31
+ private configDir: string;
32
+ private backupDir: string;
33
+ private platformInfo: any;
34
+
35
+ constructor() {
36
+ this.platformInfo = getPlatformInfo();
37
+ this.configDir = join(homedir(), '.bs9');
38
+ this.backupDir = join(this.configDir, 'backups');
39
+ this.ensureDirectories();
40
+ }
41
+
42
+ private ensureDirectories(): void {
43
+ if (!existsSync(this.configDir)) {
44
+ mkdirSync(this.configDir, { recursive: true });
45
+ }
46
+ if (!existsSync(this.backupDir)) {
47
+ mkdirSync(this.backupDir, { recursive: true });
48
+ }
49
+ }
50
+
51
+ private getCurrentVersion(): string {
52
+ try {
53
+ // Use the actual package.json path from the project root
54
+ const packageJson = join(process.cwd(), 'package.json');
55
+ const content = require('fs').readFileSync(packageJson, 'utf-8');
56
+ const pkg = JSON.parse(content);
57
+ return pkg.version;
58
+ } catch {
59
+ return '1.0.0'; // Fallback version
60
+ }
61
+ }
62
+
63
+ private async getLatestVersion(): Promise<string> {
64
+ try {
65
+ const response = await fetch('https://registry.npmjs.org/bs9/latest');
66
+ const data = await response.json();
67
+ return data.version;
68
+ } catch (error) {
69
+ console.warn('⚠️ Failed to fetch latest version from npm, using fallback');
70
+ return '1.3.0'; // Return current version as fallback
71
+ }
72
+ }
73
+
74
+ private compareVersions(v1: string, v2: string): number {
75
+ const parts1 = v1.split('.').map(Number);
76
+ const parts2 = v2.split('.').map(Number);
77
+
78
+ for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
79
+ const part1 = parts1[i] || 0;
80
+ const part2 = parts2[i] || 0;
81
+
82
+ if (part1 > part2) return 1;
83
+ if (part1 < part2) return -1;
84
+ }
85
+
86
+ return 0;
87
+ }
88
+
89
+ public async getUpdateInfo(): Promise<UpdateInfo> {
90
+ const currentVersion = this.getCurrentVersion();
91
+ const latestVersion = await this.getLatestVersion();
92
+
93
+ return {
94
+ currentVersion,
95
+ latestVersion,
96
+ hasUpdate: this.compareVersions(currentVersion, latestVersion) > 0
97
+ };
98
+ }
99
+
100
+ private createBackup(): BackupInfo {
101
+ const timestamp = Date.now();
102
+ const currentVersion = this.getCurrentVersion();
103
+ const backupName = `backup-${currentVersion}-${timestamp}`;
104
+ const backupPath = join(this.backupDir, backupName);
105
+
106
+ mkdirSync(backupPath, { recursive: true });
107
+
108
+ // Backup key files
109
+ const filesToBackup = [
110
+ 'bin/bs9',
111
+ 'package.json',
112
+ 'src',
113
+ 'README.md',
114
+ 'LICENSE'
115
+ ];
116
+
117
+ const backedUpFiles: string[] = [];
118
+
119
+ for (const file of filesToBackup) {
120
+ const sourcePath = join(process.cwd(), file);
121
+ const targetPath = join(backupPath, file);
122
+
123
+ try {
124
+ if (existsSync(sourcePath)) {
125
+ execSync(`cp -r "${sourcePath}" "${targetPath}"`, { stdio: 'ignore' });
126
+ backedUpFiles.push(file);
127
+ }
128
+ } catch (error) {
129
+ console.warn(`⚠️ Failed to backup ${file}: ${error}`);
130
+ }
131
+ }
132
+
133
+ return {
134
+ version: currentVersion,
135
+ timestamp,
136
+ files: backedUpFiles
137
+ };
138
+ }
139
+
140
+ public async performUpdate(targetVersion?: string): Promise<void> {
141
+ console.log('🔄 Starting BS9 update...');
142
+
143
+ // Create backup
144
+ console.log('📦 Creating backup...');
145
+ const backup = this.createBackup();
146
+ console.log(`✅ Backup created: ${backup.version}-${backup.timestamp}`);
147
+
148
+ // Update package.json version
149
+ const packageJsonPath = join(process.cwd(), 'package.json');
150
+ const packageJson = JSON.parse(require('fs').readFileSync(packageJsonPath, 'utf-8'));
151
+ const oldVersion = packageJson.version;
152
+ packageJson.version = targetVersion || await this.getLatestVersion();
153
+
154
+ require('fs').writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
155
+ console.log(`📝 Updated version: ${oldVersion} → ${packageJson.version}`);
156
+
157
+ // Install dependencies
158
+ console.log('📦 Installing dependencies...');
159
+ try {
160
+ execSync('bun install', { stdio: 'inherit', cwd: process.cwd() });
161
+ console.log('✅ Dependencies installed');
162
+ } catch (error) {
163
+ console.error('❌ Failed to install dependencies:', error);
164
+ await this.rollback(backup);
165
+ throw error;
166
+ }
167
+
168
+ // Rebuild
169
+ console.log('🔨 Rebuilding BS9...');
170
+ try {
171
+ execSync('bun run build', { stdio: 'inherit', cwd: process.cwd() });
172
+ console.log('✅ Build completed');
173
+ } catch (error) {
174
+ console.error('❌ Build failed:', error);
175
+ await this.rollback(backup);
176
+ throw error;
177
+ }
178
+
179
+ // Save backup info
180
+ const backupInfoPath = join(this.backupDir, `current-backup.json`);
181
+ writeFileSync(backupInfoPath, JSON.stringify(backup, null, 2));
182
+
183
+ console.log('🎉 BS9 updated successfully!');
184
+ console.log(` Version: ${packageJson.version}`);
185
+ console.log(` Backup: ${backup.version}-${backup.timestamp}`);
186
+ }
187
+
188
+ public async rollback(backup?: BackupInfo): Promise<void> {
189
+ console.log('🔄 Rolling back BS9...');
190
+
191
+ let backupInfo: BackupInfo;
192
+ if (backup) {
193
+ backupInfo = backup;
194
+ } else {
195
+ // Load latest backup
196
+ const backupInfoPath = join(this.backupDir, 'current-backup.json');
197
+ if (!existsSync(backupInfoPath)) {
198
+ console.error('❌ No backup found for rollback');
199
+ return;
200
+ }
201
+ backupInfo = JSON.parse(require('fs').readFileSync(backupInfoPath, 'utf-8'));
202
+ }
203
+
204
+ const backupPath = join(this.backupDir, `backup-${backupInfo.version}-${backupInfo.timestamp}`);
205
+
206
+ if (!existsSync(backupPath)) {
207
+ console.error(`❌ Backup not found: ${backupPath}`);
208
+ return;
209
+ }
210
+
211
+ // Restore files
212
+ for (const file of backupInfo.files) {
213
+ const sourcePath = join(backupPath, file);
214
+ const targetPath = join(process.cwd(), file);
215
+
216
+ try {
217
+ execSync(`cp -r "${sourcePath}" "${targetPath}"`, { stdio: 'ignore' });
218
+ console.log(`✅ Restored ${file}`);
219
+ } catch (error) {
220
+ console.warn(`⚠️ Failed to restore ${file}: ${error}`);
221
+ }
222
+ }
223
+
224
+ // Reinstall dependencies
225
+ console.log('📦 Reinstalling dependencies...');
226
+ try {
227
+ execSync('bun install', { stdio: 'inherit', cwd: process.cwd() });
228
+ console.log('✅ Dependencies reinstalled');
229
+ } catch (error) {
230
+ console.error('❌ Failed to reinstall dependencies:', error);
231
+ }
232
+
233
+ console.log(`🔄 Rollback to version ${backupInfo.version} completed`);
234
+ }
235
+
236
+ private listBackups(): void {
237
+ console.log('📋 BS9 Backup History:');
238
+ console.log('='.repeat(50));
239
+
240
+ try {
241
+ const backups = execSync(`ls -la "${this.backupDir}" | grep backup-`, { encoding: 'utf-8' });
242
+ const lines = backups.trim().split('\n');
243
+
244
+ if (lines.length === 0) {
245
+ console.log('No backups found.');
246
+ return;
247
+ }
248
+
249
+ for (const line of lines) {
250
+ const parts = line.trim().split(/\s+/);
251
+ const backupName = parts[parts.length - 1];
252
+ const match = backupName.match(/backup-(.+)-(\d+)/);
253
+
254
+ if (match) {
255
+ const [, version, timestamp] = match;
256
+ const date = new Date(parseInt(timestamp));
257
+ console.log(`${version.padEnd(10)} ${date.toISOString()} ${backupName}`);
258
+ }
259
+ }
260
+ } catch (error) {
261
+ console.error('❌ Failed to list backups:', error);
262
+ }
263
+ }
264
+
265
+ public async checkForUpdates(options?: { check?: boolean; force?: boolean }): Promise<void> {
266
+ console.log('🔍 Checking for BS9 updates...');
267
+ const updateInfo = await this.getUpdateInfo();
268
+
269
+ console.log(`Current version: ${updateInfo.currentVersion}`);
270
+ console.log(`Latest version: ${updateInfo.latestVersion}`);
271
+
272
+ if (!options?.force && !updateInfo.hasUpdate) {
273
+ console.log('✅ BS9 is up to date');
274
+ console.log(` Current version: ${updateInfo.currentVersion}`);
275
+ return;
276
+ }
277
+
278
+ if (updateInfo.hasUpdate) {
279
+ console.log('✨ Update available!');
280
+ console.log(` Run: bs9 update to install ${updateInfo.latestVersion}`);
281
+ } else {
282
+ console.log('✅ BS9 is up to date');
283
+ console.log(` Current version: ${updateInfo.currentVersion}`);
284
+ }
285
+ }
286
+ }
287
+
288
+ export async function updateCommand(options: UpdateOptions): Promise<void> {
289
+ const updater = new BS9Updater();
290
+
291
+ try {
292
+ if (options.check) {
293
+ await updater.checkForUpdates(options);
294
+ return;
295
+ }
296
+
297
+ if (options.rollback) {
298
+ await updater.rollback();
299
+ return;
300
+ }
301
+
302
+ // Check for updates first
303
+ const updateInfo = await updater.getUpdateInfo();
304
+
305
+ if (!options.force && !updateInfo.hasUpdate) {
306
+ console.log('✅ BS9 is already up to date');
307
+ console.log(` Current version: ${updateInfo.currentVersion}`);
308
+ return;
309
+ }
310
+
311
+ if (updateInfo.hasUpdate) {
312
+ console.log(`📦 Update available: ${updateInfo.currentVersion} → ${updateInfo.latestVersion}`);
313
+ }
314
+
315
+ // Perform update
316
+ await updater.performUpdate(options.version);
317
+
318
+ } catch (error) {
319
+ console.error('❌ Update failed:', error);
320
+ process.exit(1);
321
+ }
322
+ }
@@ -0,0 +1,285 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { execSync } from "node:child_process";
4
+
5
+ interface ConsulService {
6
+ ID: string;
7
+ Name: string;
8
+ Tags: string[];
9
+ Address: string;
10
+ Port: number;
11
+ EnableTagOverride: boolean;
12
+ Meta: Record<string, string>;
13
+ }
14
+
15
+ interface ConsulHealthCheck {
16
+ HTTP?: string;
17
+ TCP?: string;
18
+ Interval: string;
19
+ Timeout: string;
20
+ DeregisterCriticalServiceAfter: string;
21
+ }
22
+
23
+ interface ConsulRegistration {
24
+ Name: string;
25
+ ID: string;
26
+ Tags: string[];
27
+ Address: string;
28
+ Port: number;
29
+ EnableTagOverride: boolean;
30
+ Check: ConsulHealthCheck;
31
+ Meta?: Record<string, string>;
32
+ }
33
+
34
+ class ConsulServiceDiscovery {
35
+ private consulUrl: string;
36
+ private serviceName: string = '';
37
+ private serviceId: string = '';
38
+ private registeredServices: Map<string, ConsulService> = new Map();
39
+
40
+ constructor(consulUrl: string = 'http://localhost:8500') {
41
+ this.consulUrl = consulUrl;
42
+ }
43
+
44
+ async registerService(config: {
45
+ name: string;
46
+ id: string;
47
+ address: string;
48
+ port: number;
49
+ tags?: string[];
50
+ healthCheck?: string;
51
+ meta?: Record<string, string>;
52
+ }): Promise<void> {
53
+ const registration: ConsulRegistration = {
54
+ Name: config.name,
55
+ ID: config.id,
56
+ Tags: config.tags || ['bs9', 'service'],
57
+ Address: config.address,
58
+ Port: config.port,
59
+ EnableTagOverride: false,
60
+ Check: {
61
+ HTTP: config.healthCheck || `http://${config.address}:${config.port}/healthz`,
62
+ Interval: '10s',
63
+ Timeout: '5s',
64
+ DeregisterCriticalServiceAfter: '30s'
65
+ },
66
+ Meta: config.meta || {}
67
+ };
68
+
69
+ try {
70
+ const response = await fetch(`${this.consulUrl}/v1/agent/service/register`, {
71
+ method: 'PUT',
72
+ headers: { 'Content-Type': 'application/json' },
73
+ body: JSON.stringify(registration)
74
+ });
75
+
76
+ if (response.ok) {
77
+ console.log(`✅ Service ${config.name} registered with Consul`);
78
+ this.registeredServices.set(config.id, {
79
+ ID: config.id,
80
+ Name: config.name,
81
+ Tags: registration.Tags,
82
+ Address: config.address,
83
+ Port: config.port,
84
+ EnableTagOverride: registration.EnableTagOverride,
85
+ Meta: registration.Meta || {}
86
+ });
87
+ } else {
88
+ throw new Error(`Failed to register service: ${response.statusText}`);
89
+ }
90
+ } catch (error) {
91
+ console.error(`❌ Consul registration failed: ${error}`);
92
+ throw error;
93
+ }
94
+ }
95
+
96
+ async deregisterService(serviceId: string): Promise<void> {
97
+ try {
98
+ const response = await fetch(`${this.consulUrl}/v1/agent/service/deregister/${serviceId}`, {
99
+ method: 'PUT'
100
+ });
101
+
102
+ if (response.ok) {
103
+ console.log(`✅ Service ${serviceId} deregistered from Consul`);
104
+ this.registeredServices.delete(serviceId);
105
+ } else {
106
+ throw new Error(`Failed to deregister service: ${response.statusText}`);
107
+ }
108
+ } catch (error) {
109
+ console.error(`❌ Consul deregistration failed: ${error}`);
110
+ throw error;
111
+ }
112
+ }
113
+
114
+ async discoverServices(serviceName?: string): Promise<ConsulService[]> {
115
+ try {
116
+ let url = `${this.consulUrl}/v1/agent/services`;
117
+ if (serviceName) {
118
+ url += `?service=${serviceName}`;
119
+ }
120
+
121
+ const response = await fetch(url);
122
+ if (!response.ok) {
123
+ throw new Error(`Failed to discover services: ${response.statusText}`);
124
+ }
125
+
126
+ const services: Record<string, ConsulService> = await response.json();
127
+ return Object.values(services).filter(service =>
128
+ service.Tags.includes('bs9')
129
+ );
130
+ } catch (error) {
131
+ console.error(`❌ Service discovery failed: ${error}`);
132
+ return [];
133
+ }
134
+ }
135
+
136
+ async getServiceHealth(serviceName: string): Promise<any[]> {
137
+ try {
138
+ const response = await fetch(`${this.consulUrl}/v1/health/service/${serviceName}`);
139
+ if (!response.ok) {
140
+ throw new Error(`Failed to get service health: ${response.statusText}`);
141
+ }
142
+
143
+ return await response.json();
144
+ } catch (error) {
145
+ console.error(`❌ Health check failed: ${error}`);
146
+ return [];
147
+ }
148
+ }
149
+
150
+ async watchService(serviceName: string, callback: (services: ConsulService[]) => void): Promise<void> {
151
+ let lastServices: ConsulService[] = [];
152
+
153
+ const checkServices = async () => {
154
+ try {
155
+ const services = await this.discoverServices(serviceName);
156
+
157
+ // Check if services have changed
158
+ const servicesChanged = JSON.stringify(services) !== JSON.stringify(lastServices);
159
+
160
+ if (servicesChanged) {
161
+ callback(services);
162
+ lastServices = services;
163
+ }
164
+ } catch (error) {
165
+ console.error(`❌ Service watch error: ${error}`);
166
+ }
167
+ };
168
+
169
+ // Initial check
170
+ await checkServices();
171
+
172
+ // Set up periodic checking
173
+ setInterval(checkServices, 5000);
174
+ }
175
+
176
+ getRegisteredServices(): ConsulService[] {
177
+ return Array.from(this.registeredServices.values());
178
+ }
179
+
180
+ async isConsulAvailable(): Promise<boolean> {
181
+ try {
182
+ const response = await fetch(`${this.consulUrl}/v1/status/leader`);
183
+ return response.ok;
184
+ } catch {
185
+ return false;
186
+ }
187
+ }
188
+ }
189
+
190
+ export async function consulCommand(action: string, options: any): Promise<void> {
191
+ const consul = new ConsulServiceDiscovery(options.consulUrl);
192
+
193
+ try {
194
+ switch (action) {
195
+ case 'register':
196
+ if (!options.name || !options.id || !options.address || !options.port) {
197
+ console.error('❌ --name, --id, --address, and --port are required for register');
198
+ process.exit(1);
199
+ }
200
+
201
+ await consul.registerService({
202
+ name: options.name,
203
+ id: options.id,
204
+ address: options.address,
205
+ port: parseInt(options.port),
206
+ tags: options.tags ? options.tags.split(',') : undefined,
207
+ healthCheck: options.healthCheck,
208
+ meta: options.meta ? JSON.parse(options.meta) : undefined
209
+ });
210
+ break;
211
+
212
+ case 'deregister':
213
+ if (!options.id) {
214
+ console.error('❌ --id is required for deregister');
215
+ process.exit(1);
216
+ }
217
+ await consul.deregisterService(options.id);
218
+ break;
219
+
220
+ case 'discover':
221
+ const services = await consul.discoverServices(options.service);
222
+ console.log('🔍 Discovered Services:');
223
+ console.log('='.repeat(50));
224
+
225
+ if (services.length === 0) {
226
+ console.log('No BS9 services found in Consul');
227
+ } else {
228
+ services.forEach(service => {
229
+ console.log(`📦 ${service.Name} (${service.ID})`);
230
+ console.log(` Address: ${service.Address}:${service.Port}`);
231
+ console.log(` Tags: ${service.Tags.join(', ')}`);
232
+ console.log(` Health: ${service.Meta?.health || 'unknown'}`);
233
+ console.log('');
234
+ });
235
+ }
236
+ break;
237
+
238
+ case 'health':
239
+ if (!options.service) {
240
+ console.error('❌ --service is required for health check');
241
+ process.exit(1);
242
+ }
243
+
244
+ const health = await consul.getServiceHealth(options.service);
245
+ console.log(`🏥 Health Status for ${options.service}:`);
246
+ console.log('='.repeat(50));
247
+
248
+ if (health.length === 0) {
249
+ console.log('No health information available');
250
+ } else {
251
+ health.forEach(check => {
252
+ const status = check.Checks?.[0]?.Status || 'unknown';
253
+ const output = check.Checks?.[0]?.Output || 'No output';
254
+
255
+ console.log(`📦 ${check.Service.ID}`);
256
+ console.log(` Status: ${status}`);
257
+ console.log(` Node: ${check.Node.Node}`);
258
+ console.log(` Address: ${check.Service.Address}:${check.Service.Port}`);
259
+ console.log(` Output: ${output}`);
260
+ console.log('');
261
+ });
262
+ }
263
+ break;
264
+
265
+ case 'status':
266
+ const available = await consul.isConsulAvailable();
267
+ if (available) {
268
+ console.log('✅ Consul is available');
269
+ console.log(` URL: ${options.consulUrl || 'http://localhost:8500'}`);
270
+ } else {
271
+ console.log('❌ Consul is not available');
272
+ console.log(` URL: ${options.consulUrl || 'http://localhost:8500'}`);
273
+ }
274
+ break;
275
+
276
+ default:
277
+ console.error(`❌ Unknown action: ${action}`);
278
+ console.log('Available actions: register, deregister, discover, health, status');
279
+ process.exit(1);
280
+ }
281
+ } catch (error) {
282
+ console.error(`❌ Consul command failed: ${error}`);
283
+ process.exit(1);
284
+ }
285
+ }
@@ -0,0 +1,341 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { serve } from "bun";
4
+ import { randomBytes } from "node:crypto";
5
+
6
+ interface AdvancedWidget {
7
+ id: string;
8
+ type: 'metric' | 'chart' | 'alert' | 'custom';
9
+ title: string;
10
+ config: Record<string, any>;
11
+ position: { x: number; y: number; width: number; height: number };
12
+ }
13
+
14
+ interface AnomalyDetection {
15
+ service: string;
16
+ metric: string;
17
+ baseline: number;
18
+ threshold: number;
19
+ detected: boolean;
20
+ timestamp: number;
21
+ }
22
+
23
+ interface AlertCorrelation {
24
+ id: string;
25
+ alerts: Array<{
26
+ service: string;
27
+ type: string;
28
+ severity: 'low' | 'medium' | 'high' | 'critical';
29
+ timestamp: number;
30
+ }>;
31
+ correlation: number;
32
+ rootCause?: string;
33
+ }
34
+
35
+ class AdvancedMonitoring {
36
+ private widgets: Map<string, AdvancedWidget> = new Map();
37
+ private anomalies: Map<string, AnomalyDetection> = new Map();
38
+ private correlations: Map<string, AlertCorrelation> = new Map();
39
+ private baselines: Map<string, number> = new Map();
40
+
41
+ constructor() {
42
+ this.initializeDefaultWidgets();
43
+ }
44
+
45
+ private initializeDefaultWidgets(): void {
46
+ // CPU Usage Widget
47
+ this.widgets.set('cpu-usage', {
48
+ id: 'cpu-usage',
49
+ type: 'metric',
50
+ title: 'CPU Usage',
51
+ config: {
52
+ metric: 'cpu',
53
+ unit: '%',
54
+ threshold: 80,
55
+ refreshInterval: 5000
56
+ },
57
+ position: { x: 0, y: 0, width: 300, height: 200 }
58
+ });
59
+
60
+ // Memory Usage Widget
61
+ this.widgets.set('memory-usage', {
62
+ id: 'memory-usage',
63
+ type: 'metric',
64
+ title: 'Memory Usage',
65
+ config: {
66
+ metric: 'memory',
67
+ unit: 'MB',
68
+ threshold: 90,
69
+ refreshInterval: 5000
70
+ },
71
+ position: { x: 320, y: 0, width: 300, height: 200 }
72
+ });
73
+
74
+ // Service Health Chart
75
+ this.widgets.set('service-health', {
76
+ id: 'service-health',
77
+ type: 'chart',
78
+ title: 'Service Health',
79
+ config: {
80
+ chartType: 'line',
81
+ timeRange: '1h',
82
+ metrics: ['cpu', 'memory', 'response_time'],
83
+ refreshInterval: 10000
84
+ },
85
+ position: { x: 0, y: 220, width: 620, height: 300 }
86
+ });
87
+
88
+ // Alert Status Widget
89
+ this.widgets.set('alert-status', {
90
+ id: 'alert-status',
91
+ type: 'alert',
92
+ title: 'Active Alerts',
93
+ config: {
94
+ severity: ['high', 'critical'],
95
+ refreshInterval: 3000
96
+ },
97
+ position: { x: 640, y: 0, width: 300, height: 200 }
98
+ });
99
+ }
100
+
101
+ addWidget(widget: AdvancedWidget): void {
102
+ this.widgets.set(widget.id, widget);
103
+ }
104
+
105
+ removeWidget(widgetId: string): void {
106
+ this.widgets.delete(widgetId);
107
+ }
108
+
109
+ updateWidget(widgetId: string, updates: Partial<AdvancedWidget>): void {
110
+ const widget = this.widgets.get(widgetId);
111
+ if (widget) {
112
+ Object.assign(widget, updates);
113
+ }
114
+ }
115
+
116
+ getWidgets(): AdvancedWidget[] {
117
+ return Array.from(this.widgets.values());
118
+ }
119
+
120
+ async detectAnomalies(serviceMetrics: Record<string, any>): Promise<AnomalyDetection[]> {
121
+ const anomalies: AnomalyDetection[] = [];
122
+
123
+ for (const [serviceName, metrics] of Object.entries(serviceMetrics)) {
124
+ for (const [metricName, value] of Object.entries(metrics)) {
125
+ const key = `${serviceName}-${metricName}`;
126
+ const baseline = this.baselines.get(key) || 0;
127
+
128
+ // Simple anomaly detection using statistical deviation
129
+ const deviation = Math.abs((value as number) - baseline) / baseline;
130
+ const threshold = 0.3; // 30% deviation threshold
131
+
132
+ if (deviation > threshold) {
133
+ const anomaly: AnomalyDetection = {
134
+ service: serviceName,
135
+ metric: metricName,
136
+ baseline,
137
+ threshold: baseline * (1 + threshold),
138
+ detected: true,
139
+ timestamp: Date.now()
140
+ };
141
+
142
+ anomalies.push(anomaly);
143
+ this.anomalies.set(key, anomaly);
144
+ }
145
+ }
146
+ }
147
+
148
+ return anomalies;
149
+ }
150
+
151
+ async correlateAlerts(alerts: Array<any>): Promise<AlertCorrelation[]> {
152
+ const correlations: AlertCorrelation[] = [];
153
+
154
+ // Simple correlation based on time proximity and service relationships
155
+ for (let i = 0; i < alerts.length; i++) {
156
+ for (let j = i + 1; j < alerts.length; j++) {
157
+ const alert1 = alerts[i];
158
+ const alert2 = alerts[j];
159
+
160
+ const timeDiff = Math.abs(alert1.timestamp - alert2.timestamp);
161
+ const timeThreshold = 60000; // 1 minute
162
+
163
+ if (timeDiff < timeThreshold) {
164
+ const correlation: AlertCorrelation = {
165
+ id: randomBytes(8).toString('hex'),
166
+ alerts: [alert1, alert2],
167
+ correlation: 1 - (timeDiff / timeThreshold)
168
+ };
169
+
170
+ correlations.push(correlation);
171
+ this.correlations.set(correlation.id, correlation);
172
+ }
173
+ }
174
+ }
175
+
176
+ return correlations;
177
+ }
178
+
179
+ updateBaseline(service: string, metric: string, value: number): void {
180
+ const key = `${service}-${metric}`;
181
+ const currentBaseline = this.baselines.get(key) || 0;
182
+
183
+ // Exponential moving average for baseline
184
+ const alpha = 0.1;
185
+ const newBaseline = alpha * (value as number) + (1 - alpha) * currentBaseline;
186
+
187
+ this.baselines.set(key, newBaseline);
188
+ }
189
+
190
+ getAnomalies(): AnomalyDetection[] {
191
+ return Array.from(this.anomalies.values());
192
+ }
193
+
194
+ getCorrelations(): AlertCorrelation[] {
195
+ return Array.from(this.correlations.values());
196
+ }
197
+
198
+ generateDashboard(): string {
199
+ const widgets = this.getWidgets();
200
+
201
+ return `
202
+ <!DOCTYPE html>
203
+ <html lang="en">
204
+ <head>
205
+ <meta charset="UTF-8">
206
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
207
+ <title>BS9 Advanced Monitoring Dashboard</title>
208
+ <style>
209
+ body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; margin: 0; padding: 20px; background: #f5f5f5; }
210
+ .dashboard { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; max-width: 1400px; margin: 0 auto; }
211
+ .widget { background: white; border-radius: 8px; padding: 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
212
+ .widget-header { font-size: 18px; font-weight: 600; margin-bottom: 15px; color: #333; }
213
+ .widget-content { min-height: 150px; }
214
+ .metric-value { font-size: 36px; font-weight: bold; color: #007bff; }
215
+ .metric-unit { font-size: 14px; color: #666; margin-left: 5px; }
216
+ .alert-item { padding: 10px; margin: 5px 0; border-left: 4px solid #dc3545; background: #f8d7da; }
217
+ .anomaly-item { padding: 10px; margin: 5px 0; border-left: 4px solid #ffc107; background: #fff3cd; }
218
+ .chart-container { height: 200px; background: #f8f9fa; border-radius: 4px; display: flex; align-items: center; justify-content: center; }
219
+ </style>
220
+ </head>
221
+ <body>
222
+ <h1>🔍 BS9 Advanced Monitoring Dashboard</h1>
223
+
224
+ <div class="dashboard">
225
+ ${widgets.map(widget => this.renderWidget(widget)).join('')}
226
+ </div>
227
+
228
+ <script>
229
+ // Auto-refresh widgets
230
+ setInterval(() => {
231
+ fetch('/api/advanced-metrics')
232
+ .then(response => response.json())
233
+ .then(data => {
234
+ updateWidgets(data);
235
+ })
236
+ .catch(error => console.error('Failed to fetch metrics:', error));
237
+ }, 5000);
238
+
239
+ function updateWidgets(data) {
240
+ // Update widget content based on data
241
+ console.log('Updating widgets with data:', data);
242
+ }
243
+ </script>
244
+ </body>
245
+ </html>`;
246
+ }
247
+
248
+ private renderWidget(widget: AdvancedWidget): string {
249
+ switch (widget.type) {
250
+ case 'metric':
251
+ return `
252
+ <div class="widget" style="grid-column: span ${Math.ceil(widget.position.width / 300)};">
253
+ <div class="widget-header">${widget.title}</div>
254
+ <div class="widget-content">
255
+ <div class="metric-value" id="${widget.id}-value">${typeof widget.config.value === 'number' ? widget.config.value : '--'}</div>
256
+ <span class="metric-unit">${widget.config.unit}</span>
257
+ </div>
258
+ </div>
259
+ `;
260
+
261
+ case 'chart':
262
+ return `
263
+ <div class="widget" style="grid-column: span ${Math.ceil(widget.position.width / 300)};">
264
+ <div class="widget-header">${widget.title}</div>
265
+ <div class="widget-content">
266
+ <div class="chart-container">
267
+ 📊 ${widget.config.chartType.toUpperCase()} Chart
268
+ </div>
269
+ </div>
270
+ </div>
271
+ `;
272
+
273
+ case 'alert':
274
+ return `
275
+ <div class="widget" style="grid-column: span ${Math.ceil(widget.position.width / 300)};">
276
+ <div class="widget-header">${widget.title}</div>
277
+ <div class="widget-content" id="${widget.id}-alerts">
278
+ <div class="alert-item">No active alerts</div>
279
+ </div>
280
+ </div>
281
+ `;
282
+
283
+ default:
284
+ return `
285
+ <div class="widget">
286
+ <div class="widget-header">${widget.title}</div>
287
+ <div class="widget-content">
288
+ Custom widget content
289
+ </div>
290
+ </div>
291
+ `;
292
+ }
293
+ }
294
+ }
295
+
296
+ export async function advancedMonitoringCommand(options: any): Promise<void> {
297
+ const monitoring = new AdvancedMonitoring();
298
+
299
+ console.log('🔍 Starting Advanced Monitoring Dashboard...');
300
+ console.log('📊 Features:');
301
+ console.log(' - Custom widgets');
302
+ console.log(' - Anomaly detection');
303
+ console.log(' - Alert correlation');
304
+ console.log(' - Performance baselines');
305
+
306
+ const server = serve({
307
+ port: options.port || 8090,
308
+ async fetch(req) {
309
+ const url = new URL(req.url);
310
+
311
+ if (url.pathname === '/') {
312
+ return new Response(monitoring.generateDashboard(), {
313
+ headers: { 'Content-Type': 'text/html' }
314
+ });
315
+ }
316
+
317
+ if (url.pathname === '/api/advanced-metrics') {
318
+ // Mock advanced metrics data
319
+ const metrics = {
320
+ widgets: monitoring.getWidgets(),
321
+ anomalies: monitoring.getAnomalies(),
322
+ correlations: monitoring.getCorrelations(),
323
+ timestamp: Date.now()
324
+ };
325
+
326
+ return new Response(JSON.stringify(metrics), {
327
+ headers: { 'Content-Type': 'application/json' }
328
+ });
329
+ }
330
+
331
+ return new Response('Not Found', { status: 404 });
332
+ }
333
+ });
334
+
335
+ console.log(`✅ Advanced monitoring dashboard started on http://localhost:${options.port || 8090}`);
336
+ console.log('🔗 Features available:');
337
+ console.log(' - Real-time anomaly detection');
338
+ console.log(' - Alert correlation analysis');
339
+ console.log(' - Custom widget configuration');
340
+ console.log(' - Performance baseline tracking');
341
+ }