mcp-ssh-manager 3.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.
Files changed (56) hide show
  1. package/.claude/settings.local.json +87 -0
  2. package/.flake8 +15 -0
  3. package/.hooks-config.json +131 -0
  4. package/.pre-commit-config.yaml +105 -0
  5. package/CHANGELOG.md +137 -0
  6. package/CLAUDE.md +190 -0
  7. package/CONTRIBUTING.md +114 -0
  8. package/DESCRIPTION +1 -0
  9. package/INSTALLATION.md +180 -0
  10. package/LICENSE +21 -0
  11. package/QUICKSTART.md +77 -0
  12. package/README.md +718 -0
  13. package/cli/README.md +286 -0
  14. package/cli/build.sh +199 -0
  15. package/cli/commands/server.sh +284 -0
  16. package/cli/demo.sh +107 -0
  17. package/cli/install.sh +164 -0
  18. package/cli/lib/colors.sh +174 -0
  19. package/cli/lib/config.sh +284 -0
  20. package/cli/lib/menu.sh +430 -0
  21. package/cli/migrate.sh +168 -0
  22. package/cli/ssh-manager +525 -0
  23. package/docs/ALIASES_AND_HOOKS.md +285 -0
  24. package/docs/BACKUP_GUIDE.md +500 -0
  25. package/docs/DEPLOYMENT_GUIDE.md +241 -0
  26. package/docs/SEO.md +257 -0
  27. package/examples/backup-workflow.js +481 -0
  28. package/examples/claude-code-config.example.json +40 -0
  29. package/examples/codex-ssh-config.example.toml +71 -0
  30. package/examples/deploy-workflow.py +183 -0
  31. package/glama.json +6 -0
  32. package/package.json +85 -0
  33. package/profiles/README.md +40 -0
  34. package/profiles/default.json +52 -0
  35. package/profiles/docker.json +62 -0
  36. package/profiles/frappe.json +107 -0
  37. package/profiles/nodejs.json +63 -0
  38. package/pyproject.toml +17 -0
  39. package/scripts/setup-hooks.sh +60 -0
  40. package/scripts/validate.sh +76 -0
  41. package/src/backup-manager.js +469 -0
  42. package/src/command-aliases.js +151 -0
  43. package/src/config-loader.js +281 -0
  44. package/src/database-manager.js +555 -0
  45. package/src/deploy-helper.js +176 -0
  46. package/src/health-monitor.js +428 -0
  47. package/src/hooks-system.js +327 -0
  48. package/src/index.js +4499 -0
  49. package/src/logger.js +276 -0
  50. package/src/profile-loader.js +187 -0
  51. package/src/server-aliases.js +150 -0
  52. package/src/server-groups.js +409 -0
  53. package/src/session-manager.js +410 -0
  54. package/src/ssh-key-manager.js +378 -0
  55. package/src/ssh-manager.js +435 -0
  56. package/src/tunnel-manager.js +576 -0
@@ -0,0 +1,327 @@
1
+ /**
2
+ * Hooks System for MCP SSH Manager
3
+ * Provides automation through pre/post execution hooks
4
+ */
5
+
6
+ import fs from 'fs';
7
+ import path from 'path';
8
+ import { exec } from 'child_process';
9
+ import { promisify } from 'util';
10
+ import { fileURLToPath } from 'url';
11
+ import { loadProfile } from './profile-loader.js';
12
+
13
+ const execAsync = promisify(exec);
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ const __dirname = path.dirname(__filename);
16
+
17
+ const HOOKS_CONFIG_FILE = path.join(__dirname, '..', '.hooks-config.json');
18
+ const HOOKS_DIR = path.join(__dirname, '..', 'hooks');
19
+
20
+ // Get hooks from the active profile
21
+ let profileHooks = {};
22
+ try {
23
+ const profile = loadProfile();
24
+ profileHooks = profile.hooks || {};
25
+ } catch (error) {
26
+ console.error(`Error loading profile hooks: ${error.message}`);
27
+ }
28
+
29
+ // Default hooks configuration (minimal, can be overridden by profiles)
30
+ const DEFAULT_HOOKS = {
31
+ // Basic error handling
32
+ 'on-error': {
33
+ enabled: true,
34
+ description: 'Run when an error occurs',
35
+ actions: [
36
+ {
37
+ type: 'notification',
38
+ name: 'log-error',
39
+ command: 'echo "[$(date)] Error on {server}: {error}" >> errors.log'
40
+ }
41
+ ]
42
+ },
43
+ // SSH key change hooks
44
+ 'pre-connect-key-change': {
45
+ enabled: false,
46
+ description: 'Run before accepting a changed SSH host key',
47
+ actions: [
48
+ {
49
+ type: 'notification',
50
+ name: 'log-key-change',
51
+ command: 'echo "[$(date)] SSH key change detected for {server} ({host}:{port})" >> ssh-key-changes.log'
52
+ }
53
+ ]
54
+ },
55
+ 'post-key-update': {
56
+ enabled: false,
57
+ description: 'Run after updating an SSH host key',
58
+ actions: [
59
+ {
60
+ type: 'notification',
61
+ name: 'log-key-updated',
62
+ command: 'echo "[$(date)] SSH key {action} for {server} ({host}:{port})" >> ssh-key-changes.log'
63
+ }
64
+ ]
65
+ }
66
+ };
67
+
68
+ /**
69
+ * Initialize hooks directory and configuration
70
+ */
71
+ export async function initializeHooks() {
72
+ // Create hooks directory if it doesn't exist
73
+ if (!fs.existsSync(HOOKS_DIR)) {
74
+ fs.mkdirSync(HOOKS_DIR, { recursive: true });
75
+ }
76
+
77
+ // Merge profile hooks with defaults
78
+ const mergedHooks = { ...DEFAULT_HOOKS, ...profileHooks };
79
+
80
+ // Create hooks configuration if it doesn't exist
81
+ if (!fs.existsSync(HOOKS_CONFIG_FILE)) {
82
+ saveHooksConfig(mergedHooks);
83
+ }
84
+
85
+ return true;
86
+ }
87
+
88
+ /**
89
+ * Load hooks configuration
90
+ */
91
+ export function loadHooksConfig() {
92
+ try {
93
+ // Start with profile hooks
94
+ let hooks = { ...profileHooks };
95
+
96
+ // Merge with custom hooks from file
97
+ if (fs.existsSync(HOOKS_CONFIG_FILE)) {
98
+ const data = fs.readFileSync(HOOKS_CONFIG_FILE, 'utf8');
99
+ const customHooks = JSON.parse(data);
100
+
101
+ // Deep merge hooks
102
+ for (const [hookName, hookConfig] of Object.entries(customHooks)) {
103
+ if (hooks[hookName]) {
104
+ // Merge existing hook
105
+ hooks[hookName] = {
106
+ ...hooks[hookName],
107
+ ...hookConfig
108
+ };
109
+ } else {
110
+ // Add new hook
111
+ hooks[hookName] = hookConfig;
112
+ }
113
+ }
114
+ }
115
+
116
+ return hooks;
117
+ } catch (error) {
118
+ console.error(`Error loading hooks config: ${error.message}`);
119
+ }
120
+ return { ...DEFAULT_HOOKS, ...profileHooks };
121
+ }
122
+
123
+ /**
124
+ * Save hooks configuration
125
+ */
126
+ export function saveHooksConfig(config) {
127
+ try {
128
+ fs.writeFileSync(HOOKS_CONFIG_FILE, JSON.stringify(config, null, 2));
129
+ return true;
130
+ } catch (error) {
131
+ console.error(`Error saving hooks config: ${error.message}`);
132
+ return false;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Execute a hook
138
+ */
139
+ export async function executeHook(hookName, context = {}) {
140
+ const config = loadHooksConfig();
141
+ const hook = config[hookName];
142
+
143
+ if (!hook || !hook.enabled) {
144
+ return { success: true, skipped: true };
145
+ }
146
+
147
+ console.error(`🎣 Executing hook: ${hookName}`);
148
+ const results = [];
149
+
150
+ for (const action of hook.actions) {
151
+ try {
152
+ // Check environment variables if required
153
+ if (action.requiresEnv) {
154
+ const missingEnv = action.requiresEnv.filter(env => !process.env[env]);
155
+ if (missingEnv.length > 0) {
156
+ if (!action.optional) {
157
+ throw new Error(`Missing required environment variables: ${missingEnv.join(', ')}`);
158
+ }
159
+ console.error(` ⚠️ Skipping ${action.name}: missing env vars`);
160
+ continue;
161
+ }
162
+ }
163
+
164
+ // Replace placeholders in command
165
+ let command = action.command || action.remoteCommand || '';
166
+ for (const [key, value] of Object.entries(context)) {
167
+ command = command.replace(new RegExp(`{${key}}`, 'g'), value);
168
+ }
169
+
170
+ // Execute action based on type
171
+ const result = await executeAction(action, command, context);
172
+ results.push(result);
173
+
174
+ // Check validation results
175
+ if (action.type === 'validation' && !result.success && !action.optional) {
176
+ const errorMsg = action.errorMessage || `Validation failed: ${action.name}`;
177
+ throw new Error(errorMsg);
178
+ }
179
+
180
+ console.error(` ✅ ${action.name}: completed`);
181
+
182
+ } catch (error) {
183
+ if (!action.optional) {
184
+ console.error(` ❌ ${action.name}: ${error.message}`);
185
+ return {
186
+ success: false,
187
+ hook: hookName,
188
+ action: action.name,
189
+ error: error.message,
190
+ results
191
+ };
192
+ }
193
+ console.error(` ⚠️ ${action.name}: ${error.message} (optional, continuing)`);
194
+ }
195
+ }
196
+
197
+ return {
198
+ success: true,
199
+ hook: hookName,
200
+ results
201
+ };
202
+ }
203
+
204
+ /**
205
+ * Execute a single action
206
+ */
207
+ async function executeAction(action, command, context) {
208
+ const result = {
209
+ action: action.name,
210
+ type: action.type,
211
+ timestamp: new Date().toISOString()
212
+ };
213
+
214
+ try {
215
+ if (action.remoteCommand && context.sshConnection) {
216
+ // Execute on remote server
217
+ const output = await context.sshConnection.execCommand(command, {
218
+ cwd: context.cwd || context.defaultDir
219
+ });
220
+
221
+ result.output = output.stdout;
222
+ result.error = output.stderr;
223
+ result.success = output.code === 0;
224
+
225
+ if (action.expectEmpty && output.stdout.trim()) {
226
+ result.success = false;
227
+ }
228
+ } else if (action.command) {
229
+ // Execute locally
230
+ const { stdout, stderr } = await execAsync(command);
231
+
232
+ result.output = stdout;
233
+ result.error = stderr;
234
+ result.success = true;
235
+
236
+ if (action.expectEmpty && stdout.trim()) {
237
+ result.success = false;
238
+ }
239
+ }
240
+
241
+ // Handle specific action types
242
+ switch (action.type) {
243
+ case 'backup':
244
+ result.backupInfo = {
245
+ timestamp: new Date().toISOString(),
246
+ command: command
247
+ };
248
+ break;
249
+
250
+ case 'notification':
251
+ result.notified = true;
252
+ break;
253
+
254
+ case 'validation':
255
+ result.validated = result.success;
256
+ break;
257
+
258
+ case 'verification':
259
+ result.verified = result.success;
260
+ break;
261
+ }
262
+
263
+ } catch (error) {
264
+ result.success = false;
265
+ result.error = error.message;
266
+ }
267
+
268
+ return result;
269
+ }
270
+
271
+ /**
272
+ * Add or update a hook
273
+ */
274
+ export function addHook(hookName, hookConfig) {
275
+ const config = loadHooksConfig();
276
+ config[hookName] = hookConfig;
277
+ return saveHooksConfig(config);
278
+ }
279
+
280
+ /**
281
+ * Remove a hook
282
+ */
283
+ export function removeHook(hookName) {
284
+ const config = loadHooksConfig();
285
+ delete config[hookName];
286
+ return saveHooksConfig(config);
287
+ }
288
+
289
+ /**
290
+ * Enable/disable a hook
291
+ */
292
+ export function toggleHook(hookName, enabled) {
293
+ const config = loadHooksConfig();
294
+ if (config[hookName]) {
295
+ config[hookName].enabled = enabled;
296
+ return saveHooksConfig(config);
297
+ }
298
+ return false;
299
+ }
300
+
301
+ /**
302
+ * List all hooks
303
+ */
304
+ export function listHooks() {
305
+ const config = loadHooksConfig();
306
+ return Object.entries(config).map(([name, hook]) => ({
307
+ name,
308
+ enabled: hook.enabled,
309
+ description: hook.description,
310
+ actionCount: hook.actions ? hook.actions.length : 0
311
+ }));
312
+ }
313
+
314
+ /**
315
+ * Create a custom hook script
316
+ */
317
+ export async function createHookScript(scriptName, scriptContent) {
318
+ const scriptPath = path.join(HOOKS_DIR, scriptName);
319
+
320
+ try {
321
+ fs.writeFileSync(scriptPath, scriptContent);
322
+ fs.chmodSync(scriptPath, '755');
323
+ return scriptPath;
324
+ } catch (error) {
325
+ throw new Error(`Failed to create hook script: ${error.message}`);
326
+ }
327
+ }