easy-devops 1.1.0 → 1.2.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.
Files changed (34) hide show
  1. package/README.md +2 -2
  2. package/package.json +6 -3
  3. package/src/cli/index.js +13 -1
  4. package/src/cli/menus/dashboard.js +1 -1
  5. package/src/core/channels/discord.js +77 -0
  6. package/src/core/channels/telegram.js +120 -0
  7. package/src/core/db.js +8 -1
  8. package/src/core/domainNotifier.js +298 -0
  9. package/src/core/events.js +130 -0
  10. package/src/core/notifier.js +73 -0
  11. package/src/core/shell.js +59 -7
  12. package/src/dashboard/controllers/authController.js +29 -0
  13. package/src/dashboard/controllers/domainsController.js +416 -0
  14. package/src/dashboard/controllers/nginxController.js +128 -0
  15. package/src/dashboard/controllers/settingsController.js +335 -0
  16. package/src/dashboard/controllers/sslController.js +291 -0
  17. package/src/dashboard/public/img/icon.png +0 -0
  18. package/src/dashboard/public/img/icon_b.png +0 -0
  19. package/src/dashboard/public/js/app.js +234 -2
  20. package/src/dashboard/routes/auth.js +12 -22
  21. package/src/dashboard/routes/domains.js +11 -363
  22. package/src/dashboard/routes/nginx.js +13 -146
  23. package/src/dashboard/routes/settings.js +16 -189
  24. package/src/dashboard/routes/ssl.js +9 -276
  25. package/src/dashboard/server.js +24 -27
  26. package/src/dashboard/socketManager.js +282 -0
  27. package/src/dashboard/views/index.ejs +52 -0
  28. package/src/dashboard/views/partials/domain-form.ejs +64 -1
  29. package/src/dashboard/views/partials/domains-panel.ejs +4 -0
  30. package/src/dashboard/views/partials/login.ejs +1 -1
  31. package/src/dashboard/views/partials/logs-panel.ejs +67 -0
  32. package/src/dashboard/views/partials/settings-panel.ejs +263 -87
  33. package/src/dashboard/views/partials/sidebar.ejs +3 -1
  34. /package/src/{dashboard/routes → core/channels}/.gitkeep +0 -0
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Structured Event Type Factory
3
+ *
4
+ * This module provides factory functions for creating notification event objects
5
+ * with guaranteed field shapes. Used by socketManager.js for browser notifications
6
+ * and by notifier.js for external channel dispatch (Discord, Telegram, etc.).
7
+ *
8
+ * HARD CONSTRAINT: Zero imports — pure data construction, no side effects.
9
+ * This ensures the module is safe to import anywhere without pulling in
10
+ * platform-specific or heavyweight dependencies.
11
+ *
12
+ * FIELD CONTRACT (verified against notifier.js:sendNotification):
13
+ * - type: string (e.g., 'nginx_down', 'cert_expiry', 'domain_health')
14
+ * - subtype: string (optional, e.g., 'down', 'recovery')
15
+ * - message: string (human-readable description)
16
+ * - severity: 'danger' | 'warning' | 'success' | undefined
17
+ * - domain: string (optional, for domain-specific events)
18
+ * - timestamp: number (epoch ms)
19
+ * - id: string (unique event identifier)
20
+ */
21
+
22
+ /**
23
+ * Create an nginx-down notification event.
24
+ * Emitted when nginx service status shows running: false.
25
+ *
26
+ * @returns {Object} Event object with nginx-down notification data
27
+ */
28
+ export function createNginxDownEvent() {
29
+ return {
30
+ id: `nginx-down-${Date.now()}`,
31
+ type: 'nginx_down',
32
+ subtype: 'down',
33
+ severity: 'danger',
34
+ message: '🔴 Nginx is not running',
35
+ timestamp: Date.now(),
36
+ };
37
+ }
38
+
39
+ /**
40
+ * Create an nginx-recovered notification event.
41
+ * Emitted when nginx transitions from down → up.
42
+ *
43
+ * @returns {Object} Event object with nginx-recovered notification data
44
+ */
45
+ export function createNginxRecoveredEvent() {
46
+ return {
47
+ id: `nginx-recovered-${Date.now()}`,
48
+ type: 'nginx_down',
49
+ subtype: 'recovery',
50
+ severity: 'success',
51
+ message: '✅ Nginx is running again',
52
+ timestamp: Date.now(),
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Create a certificate expiry notification event.
58
+ * Emitted when an SSL certificate is approaching expiration (< 30 days).
59
+ *
60
+ * @param {string} domain - The domain name for the certificate
61
+ * @param {number} daysLeft - Number of days until expiration
62
+ * @returns {Object} Event object with cert-expiry notification data
63
+ */
64
+ export function createCertExpiryEvent(domain, daysLeft) {
65
+ const severity = daysLeft < 10 ? 'danger' : 'warning';
66
+ return {
67
+ id: `cert-expiry-${domain}-${Date.now()}`,
68
+ type: 'cert_expiry',
69
+ severity,
70
+ message: `🔒 SSL cert for ${domain} expires in ${daysLeft} day${daysLeft === 1 ? '' : 's'}`,
71
+ domain,
72
+ timestamp: Date.now(),
73
+ };
74
+ }
75
+
76
+ /**
77
+ * Create a domain health DOWN notification event.
78
+ * Emitted when a domain backend health check fails.
79
+ *
80
+ * @param {string} domain - The domain name
81
+ * @param {number} port - The backend port
82
+ * @returns {Object} Event object with domain health notification data
83
+ */
84
+ export function createDomainDownEvent(domain, port) {
85
+ return {
86
+ id: `domain-down-${domain}-${Date.now()}`,
87
+ type: 'domain_health',
88
+ subtype: 'down',
89
+ severity: 'danger',
90
+ message: `🔴 ${domain} (port ${port}) is not responding`,
91
+ domain,
92
+ timestamp: Date.now(),
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Create a domain health RECOVERY notification event.
98
+ * Emitted when a domain backend transitions from down → up.
99
+ *
100
+ * @param {string} domain - The domain name
101
+ * @param {number} port - The backend port
102
+ * @returns {Object} Event object with domain recovery notification data
103
+ */
104
+ export function createDomainRecoveredEvent(domain, port) {
105
+ return {
106
+ id: `domain-recovered-${domain}-${Date.now()}`,
107
+ type: 'domain_health',
108
+ subtype: 'recovery',
109
+ severity: 'success',
110
+ message: `✅ ${domain} (port ${port}) is back online`,
111
+ domain,
112
+ timestamp: Date.now(),
113
+ };
114
+ }
115
+
116
+ /**
117
+ * Create a domain health notification event (legacy wrapper).
118
+ * Emitted when a domain backend health check passes or fails.
119
+ * DEPRECATED: Use createDomainDownEvent or createDomainRecoveredEvent instead.
120
+ *
121
+ * @param {string} domain - The domain name
122
+ * @param {boolean} up - Whether the domain backend is responding
123
+ * @returns {Object} Event object with domain health notification data
124
+ */
125
+ export function createDomainHealthEvent(domain, up) {
126
+ if (up) {
127
+ return createDomainRecoveredEvent(domain, '?');
128
+ }
129
+ return createDomainDownEvent(domain, '?');
130
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Notification Dispatcher Core
3
+ *
4
+ * Named-channel registry model. Channels are stored as an array of objects:
5
+ * { id, name, type, ...typeSpecificFields }
6
+ *
7
+ * sendNotification(event, channelIds) looks up each ID, finds its type, and
8
+ * dispatches to src/core/channels/{type}.js — the send() interface is unchanged.
9
+ */
10
+
11
+ import { dbGet, dbSet } from './db.js';
12
+
13
+ const CHANNELS_KEY = 'notification_channels';
14
+
15
+ /**
16
+ * Load the channel list from SQLite.
17
+ * @returns {Array} Array of channel objects (empty array if none saved)
18
+ */
19
+ export function loadChannels() {
20
+ const stored = dbGet(CHANNELS_KEY);
21
+ return Array.isArray(stored) ? stored : [];
22
+ }
23
+
24
+ /**
25
+ * Persist the channel list to SQLite.
26
+ * @param {Array} channels
27
+ */
28
+ export function saveChannels(channels) {
29
+ dbSet(CHANNELS_KEY, channels);
30
+ }
31
+
32
+ /**
33
+ * Find a single channel by ID.
34
+ * @param {string} id
35
+ * @returns {Object|null}
36
+ */
37
+ export function getChannelById(id) {
38
+ return loadChannels().find((ch) => ch.id === id) ?? null;
39
+ }
40
+
41
+ /**
42
+ * Send a notification to a specific set of channels identified by ID.
43
+ * Each channel's send() is called in a try/catch — one failure won't block others.
44
+ * Passing null or an empty array is a no-op (dashboard-only path).
45
+ *
46
+ * @param {Object} event - Notification event (type, message, severity, domain, timestamp)
47
+ * @param {string[]|null} channelIds - Array of channel IDs to dispatch to
48
+ */
49
+ export async function sendNotification(event, channelIds = null) {
50
+ if (!channelIds || channelIds.length === 0) return;
51
+
52
+ const channels = loadChannels();
53
+
54
+ for (const id of channelIds) {
55
+ const channel = channels.find((ch) => ch.id === id);
56
+ if (!channel) continue;
57
+
58
+ try {
59
+ const mod = await import(
60
+ new URL(`./channels/${channel.type}.js`, import.meta.url)
61
+ );
62
+
63
+ if (typeof mod.send !== 'function') {
64
+ console.error(`[notifier] channel type "${channel.type}" missing send()`);
65
+ continue;
66
+ }
67
+
68
+ await mod.send(event, channel);
69
+ } catch (err) {
70
+ console.error(`[notifier] channel "${channel.name}" (${channel.type}):`, err.message);
71
+ }
72
+ }
73
+ }
package/src/core/shell.js CHANGED
@@ -6,17 +6,18 @@
6
6
  * All modules must use this utility instead of calling child_process APIs directly.
7
7
  *
8
8
  * Exported functions:
9
- * - getShell() — Returns OS-resolved shell descriptor { shell, flag }
10
- * - run(cmd, options) — Executes a command and captures output; never throws
11
- * - runLive(cmd, options) — Executes a command, streaming output to the terminal; never throws
12
- * - runInteractive(cmd, options) — Executes a command with full stdio inheritance (Linux only); never throws
9
+ * - getShell() — Returns OS-resolved shell descriptor { shell, flag }
10
+ * - run(cmd, options) — Executes a command and captures output; never throws
11
+ * - runLive(cmd, options) — Executes a command, streaming output to the terminal; never throws
12
+ * - runInteractive(cmd, options) — Executes a command with full stdio inheritance (Linux only); never throws
13
+ * - runWithStdin(cmd, argsArray, stdinData) — Spawns a process with stdin piping; never throws
13
14
  *
14
15
  * CommandResult shape (returned by run()):
15
- * { success: boolean, stdout: string, stderr: string, exitCode: number|null, command: string }
16
+ * { success: boolean, stdout: string, stderr: string, exitCode: number|null, command: string }
16
17
  *
17
18
  * CommandOptions (accepted by run() and runLive()):
18
- * { timeout?: number, cwd?: string }
19
- * Defaults: timeout = 30 000 ms, cwd = process.cwd()
19
+ * { timeout?: number, cwd?: string }
20
+ * Defaults: timeout = 30 000 ms, cwd = process.cwd()
20
21
  */
21
22
 
22
23
  import { spawn } from 'child_process';
@@ -185,3 +186,54 @@ export function runInteractive(cmd, options = {}) {
185
186
  });
186
187
  });
187
188
  }
189
+
190
+ // ─── runWithStdin ─────────────────────────────────────────────────────────────
191
+
192
+ /**
193
+ * Spawns a process directly (no shell wrapper) and pipes data to its stdin.
194
+ * Intended for commands like `sudo -S` that read input from stdin.
195
+ *
196
+ * Never throws. The returned Promise always resolves with a CommandResult.
197
+ *
198
+ * @param {string} cmd - The executable to spawn (e.g. 'sudo').
199
+ * @param {string[]} argsArray - Arguments passed directly to the executable.
200
+ * @param {string} [stdinData=''] - String to pipe to the process stdin.
201
+ * @returns {Promise<{ success: boolean, stdout: string, stderr: string, exitCode: number|null, command: string }>}
202
+ */
203
+ export function runWithStdin(cmd, argsArray, stdinData = '') {
204
+ return new Promise((resolve) => {
205
+ const child = spawn(cmd, argsArray, {
206
+ stdio: ['pipe', 'pipe', 'pipe'],
207
+ windowsHide: true,
208
+ });
209
+
210
+ let stdoutBuf = '';
211
+ let stderrBuf = '';
212
+
213
+ child.stdout.on('data', (chunk) => { stdoutBuf += chunk; });
214
+ child.stderr.on('data', (chunk) => { stderrBuf += chunk; });
215
+
216
+ child.stdin.write(stdinData);
217
+ child.stdin.end();
218
+
219
+ child.on('close', (exitCode) => {
220
+ resolve({
221
+ success: exitCode === 0,
222
+ stdout: stripAnsi(stdoutBuf).trim(),
223
+ stderr: stripAnsi(stderrBuf).trim(),
224
+ exitCode,
225
+ command: cmd,
226
+ });
227
+ });
228
+
229
+ child.on('error', (err) => {
230
+ resolve({
231
+ success: false,
232
+ stdout: '',
233
+ stderr: err.message,
234
+ exitCode: null,
235
+ command: cmd,
236
+ });
237
+ });
238
+ });
239
+ }
@@ -0,0 +1,29 @@
1
+ import { loadConfig } from '../../core/config.js';
2
+
3
+ class AuthController {
4
+ login(req, res) {
5
+ const { password } = req.body;
6
+ const { dashboardPassword } = loadConfig();
7
+ if (password === dashboardPassword) {
8
+ req.session.authenticated = true;
9
+ res.json({ ok: true });
10
+ } else {
11
+ res.status(401).json({ ok: false, error: 'Invalid password' });
12
+ }
13
+ }
14
+
15
+ check(req, res) {
16
+ if (req.session.authenticated === true) {
17
+ res.json({ authenticated: true });
18
+ } else {
19
+ res.json({ authenticated: false });
20
+ }
21
+ }
22
+
23
+ logout(req, res) {
24
+ req.session.destroy();
25
+ res.json({ ok: true });
26
+ }
27
+ }
28
+
29
+ export default AuthController;