mcp-ssh-manager 3.8.5 → 3.8.7
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/CHANGELOG.md +116 -0
- package/README.md +36 -11
- package/cli/lib/menu.sh +2 -2
- package/docs/ALIASES_AND_HOOKS.md +54 -25
- package/docs/SECURITY_MODES.md +33 -3
- package/package.json +9 -5
- package/profiles/default.json +6 -4
- package/server.json +2 -2
- package/skills/ssh-manager/SKILL.md +80 -0
- package/src/backup-manager.js +70 -1
- package/src/command-aliases.js +9 -6
- package/src/config-loader.js +5 -0
- package/src/config-paths.js +12 -0
- package/src/database-manager.js +200 -21
- package/src/deploy-helper.js +15 -7
- package/src/hooks-system.js +298 -57
- package/src/index.js +59 -27
- package/src/logger.js +43 -17
- package/src/policy.js +46 -25
- package/src/profile-loader.js +18 -12
- package/src/server-aliases.js +9 -6
- package/src/shell-quote.js +8 -0
- package/src/shell-segments.js +189 -0
- package/src/ssh-key-manager.js +123 -76
- package/src/ssh-manager.js +25 -41
- package/src/user-state.js +60 -0
- package/.hooks-config.json +0 -131
package/src/hooks-system.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Hooks System for MCP SSH Manager
|
|
3
3
|
* Provides automation through pre/post execution hooks
|
|
4
|
+
*
|
|
5
|
+
* A hook action is one of:
|
|
6
|
+
* - `log`: appends a line to a file in SSH_MANAGER_HOME, in JavaScript,
|
|
7
|
+
* with no shell involved. The default `on-error` hook is one.
|
|
8
|
+
* - `command`: a shell command run on this machine.
|
|
9
|
+
* - `remoteCommand`: a shell command run on the server, when the caller
|
|
10
|
+
* hands the hook an open connection.
|
|
11
|
+
*
|
|
12
|
+
* Context values ({server}, {error}, ...) never become part of a command's
|
|
13
|
+
* text. Up to 3.8.5 they were spliced in raw, and the default `on-error` hook
|
|
14
|
+
* put the connection error inside double quotes: a server that disconnected
|
|
15
|
+
* with `Bye $(touch /tmp/x)` ran that command on the machine running the MCP
|
|
16
|
+
* server (GHSA-759m-wfpq-xmx3). Each placeholder is now replaced by a
|
|
17
|
+
* reference to a shell variable that carries the value, and a shell does not
|
|
18
|
+
* re-parse what a variable expands to.
|
|
4
19
|
*/
|
|
5
20
|
|
|
6
21
|
import fs from 'fs';
|
|
@@ -9,13 +24,19 @@ import { exec } from 'child_process';
|
|
|
9
24
|
import { promisify } from 'util';
|
|
10
25
|
import { fileURLToPath } from 'url';
|
|
11
26
|
import { loadProfile } from './profile-loader.js';
|
|
27
|
+
import { managerHome } from './config-paths.js';
|
|
28
|
+
import { shellQuote } from './shell-quote.js';
|
|
29
|
+
import { readUserState, writeUserState } from './user-state.js';
|
|
12
30
|
|
|
13
31
|
const execAsync = promisify(exec);
|
|
14
32
|
const __filename = fileURLToPath(import.meta.url);
|
|
15
33
|
const __dirname = path.dirname(__filename);
|
|
16
34
|
|
|
17
|
-
const
|
|
18
|
-
|
|
35
|
+
const HOOKS_CONFIG_NAME = 'hooks.json';
|
|
36
|
+
// Where 3.8.5 and earlier kept the file, inside the package. Read only; see
|
|
37
|
+
// user-state.js. The package also used to ship a development copy of it,
|
|
38
|
+
// which turned on Frappe hooks for every npm user (issue #87).
|
|
39
|
+
const LEGACY_HOOKS_CONFIG_FILE = path.join(__dirname, '..', '.hooks-config.json');
|
|
19
40
|
|
|
20
41
|
// Get hooks from the active profile
|
|
21
42
|
let profileHooks = {};
|
|
@@ -34,9 +55,10 @@ const DEFAULT_HOOKS = {
|
|
|
34
55
|
description: 'Run when an error occurs',
|
|
35
56
|
actions: [
|
|
36
57
|
{
|
|
37
|
-
type: '
|
|
58
|
+
type: 'log',
|
|
38
59
|
name: 'log-error',
|
|
39
|
-
|
|
60
|
+
file: 'errors.log',
|
|
61
|
+
message: 'Error on {server}: {error}'
|
|
40
62
|
}
|
|
41
63
|
]
|
|
42
64
|
},
|
|
@@ -46,9 +68,10 @@ const DEFAULT_HOOKS = {
|
|
|
46
68
|
description: 'Run before accepting a changed SSH host key',
|
|
47
69
|
actions: [
|
|
48
70
|
{
|
|
49
|
-
type: '
|
|
71
|
+
type: 'log',
|
|
50
72
|
name: 'log-key-change',
|
|
51
|
-
|
|
73
|
+
file: 'ssh-key-changes.log',
|
|
74
|
+
message: 'SSH key change detected for {server} ({host}:{port})'
|
|
52
75
|
}
|
|
53
76
|
]
|
|
54
77
|
},
|
|
@@ -57,67 +80,274 @@ const DEFAULT_HOOKS = {
|
|
|
57
80
|
description: 'Run after updating an SSH host key',
|
|
58
81
|
actions: [
|
|
59
82
|
{
|
|
60
|
-
type: '
|
|
83
|
+
type: 'log',
|
|
61
84
|
name: 'log-key-updated',
|
|
62
|
-
|
|
85
|
+
file: 'ssh-key-changes.log',
|
|
86
|
+
message: 'SSH key {action} for {server} ({host}:{port})'
|
|
63
87
|
}
|
|
64
88
|
]
|
|
65
89
|
}
|
|
66
90
|
};
|
|
67
91
|
|
|
92
|
+
const PLACEHOLDER = /^\{([A-Za-z_][A-Za-z0-9_]*)\}/;
|
|
93
|
+
|
|
94
|
+
/** @param {any} value */
|
|
95
|
+
function isScalar(value) {
|
|
96
|
+
return ['string', 'number', 'boolean'].includes(typeof value);
|
|
97
|
+
}
|
|
98
|
+
|
|
68
99
|
/**
|
|
69
|
-
*
|
|
100
|
+
* Name of the variable that carries a context value: `server` becomes
|
|
101
|
+
* SSH_MANAGER_HOOK_SERVER, `backupId` becomes SSH_MANAGER_HOOK_BACKUP_ID.
|
|
102
|
+
* Hook authors can also read these directly.
|
|
103
|
+
* @param {string} key
|
|
70
104
|
*/
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
105
|
+
function hookVariable(key) {
|
|
106
|
+
return `SSH_MANAGER_HOOK_${key.replace(/([a-z0-9])([A-Z])/g, '$1_$2').toUpperCase()}`;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Rewrite a POSIX shell command so that every {placeholder} found in `context`
|
|
111
|
+
* becomes a reference to a variable holding the value, quoted for where it
|
|
112
|
+
* sits: `"${V}"` in plain text, `${V}` inside double quotes, and `'"${V}"'`
|
|
113
|
+
* inside single quotes. The quoting only keeps a value one word; safety does
|
|
114
|
+
* not depend on it, since an expanded variable is never parsed as code, even
|
|
115
|
+
* when a template nests quotes in ways this scanner reads imperfectly.
|
|
116
|
+
*
|
|
117
|
+
* Heredoc bodies get `${V}` when the delimiter is unquoted, since the body is
|
|
118
|
+
* expanded but not re-parsed, and the value itself with line breaks removed
|
|
119
|
+
* when it is quoted, since nothing expands there and a value cut into lines
|
|
120
|
+
* could otherwise end the heredoc early.
|
|
121
|
+
*
|
|
122
|
+
* What stays the hook author's responsibility: handing a variable to
|
|
123
|
+
* something that evaluates its text again (eval, sh -c, arithmetic).
|
|
124
|
+
*
|
|
125
|
+
* @param {string} template
|
|
126
|
+
* @param {Record<string, any>} context
|
|
127
|
+
* @returns {{command: string, values: Record<string, string>}}
|
|
128
|
+
*/
|
|
129
|
+
export function renderShellCommand(template, context = {}) {
|
|
130
|
+
/** @type {Record<string, string>} */
|
|
131
|
+
const values = {};
|
|
132
|
+
/** @type {{kind: string, parens: number}[]} */
|
|
133
|
+
const frames = [{ kind: 'plain', parens: 0 }];
|
|
134
|
+
/** @type {{delimiter: string, quoted: boolean, stripTabs: boolean}[]} */
|
|
135
|
+
const heredocs = [];
|
|
136
|
+
let out = '';
|
|
137
|
+
|
|
138
|
+
/** @param {string} text @param {number} at */
|
|
139
|
+
const placeholderAt = (text, at) => {
|
|
140
|
+
if (text[at] !== '{' || text[at - 1] === '$') return null;
|
|
141
|
+
const match = PLACEHOLDER.exec(text.slice(at));
|
|
142
|
+
return match && Object.hasOwn(context, match[1]) && isScalar(context[match[1]])
|
|
143
|
+
? { key: match[1], length: match[0].length } : null;
|
|
144
|
+
};
|
|
145
|
+
/** @param {string} key */
|
|
146
|
+
const reference = key => {
|
|
147
|
+
const name = hookVariable(key);
|
|
148
|
+
values[name] = String(context[key]);
|
|
149
|
+
return name;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// Copy the heredoc bodies that start at `start`, rendering placeholders in
|
|
153
|
+
// them, and return the index just past the last delimiter line.
|
|
154
|
+
/** @param {number} start */
|
|
155
|
+
const consumeHeredocs = start => {
|
|
156
|
+
let at = start;
|
|
157
|
+
for (const heredoc of heredocs.splice(0)) {
|
|
158
|
+
while (at < template.length) {
|
|
159
|
+
const end = template.indexOf('\n', at);
|
|
160
|
+
const line = template.slice(at, end < 0 ? template.length : end);
|
|
161
|
+
at = end < 0 ? template.length : end + 1;
|
|
162
|
+
const bare = heredoc.stripTabs ? line.replace(/^\t+/, '') : line;
|
|
163
|
+
let rendered = '';
|
|
164
|
+
for (let p = 0; p < line.length; p++) {
|
|
165
|
+
const placeholder = bare !== heredoc.delimiter && placeholderAt(line, p);
|
|
166
|
+
if (!placeholder) { rendered += line[p]; continue; }
|
|
167
|
+
rendered += heredoc.quoted
|
|
168
|
+
? String(context[placeholder.key]).replace(/[\r\n]+/g, ' ')
|
|
169
|
+
: `\${${reference(placeholder.key)}}`;
|
|
170
|
+
p += placeholder.length - 1;
|
|
171
|
+
}
|
|
172
|
+
out += rendered + (end < 0 ? '' : '\n');
|
|
173
|
+
if (bare === heredoc.delimiter) break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return at;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
for (let i = 0; i < template.length; i++) {
|
|
180
|
+
const ch = template[i];
|
|
181
|
+
const frame = frames[frames.length - 1];
|
|
182
|
+
const unquoted = frame.kind !== 'single' && frame.kind !== 'double';
|
|
183
|
+
|
|
184
|
+
if (ch === '\n' && heredocs.length && unquoted) {
|
|
185
|
+
out += ch;
|
|
186
|
+
i = consumeHeredocs(i + 1) - 1;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (unquoted && ch === '<' && template[i + 1] === '<' && template[i + 2] !== '<') {
|
|
190
|
+
// A heredoc operator: record its delimiter; the body starts on the next line.
|
|
191
|
+
let at = i + 2;
|
|
192
|
+
const stripTabs = template[at] === '-';
|
|
193
|
+
if (stripTabs) at++;
|
|
194
|
+
while (template[at] === ' ' || template[at] === '\t') at++;
|
|
195
|
+
let delimiter = '';
|
|
196
|
+
let quoted = false;
|
|
197
|
+
while (at < template.length && !/[\s;&|<>()]/.test(template[at])) {
|
|
198
|
+
const c = template[at];
|
|
199
|
+
if (c === '\'' || c === '"') {
|
|
200
|
+
const close = template.indexOf(c, at + 1);
|
|
201
|
+
if (close < 0) break;
|
|
202
|
+
delimiter += template.slice(at + 1, close);
|
|
203
|
+
quoted = true;
|
|
204
|
+
at = close + 1;
|
|
205
|
+
} else if (c === '\\') {
|
|
206
|
+
delimiter += template[at + 1] ?? '';
|
|
207
|
+
quoted = true;
|
|
208
|
+
at += 2;
|
|
209
|
+
} else {
|
|
210
|
+
delimiter += c;
|
|
211
|
+
at++;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (delimiter) heredocs.push({ delimiter, quoted, stripTabs });
|
|
215
|
+
out += template.slice(i, at);
|
|
216
|
+
i = at - 1;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (frame.kind === 'single') {
|
|
221
|
+
if (ch === '\'') frames.pop();
|
|
222
|
+
} else if (ch === '\\') {
|
|
223
|
+
out += template.slice(i, i + 2);
|
|
224
|
+
i++;
|
|
225
|
+
continue;
|
|
226
|
+
} else if (frame.kind === 'double' && ch === '"') {
|
|
227
|
+
frames.pop();
|
|
228
|
+
} else if (frame.kind !== 'double' && (ch === '\'' || ch === '"')) {
|
|
229
|
+
frames.push({ kind: ch === '\'' ? 'single' : 'double', parens: 0 });
|
|
230
|
+
} else if (ch === '`') {
|
|
231
|
+
if (frame.kind === 'backtick') frames.pop();
|
|
232
|
+
else frames.push({ kind: 'backtick', parens: 0 });
|
|
233
|
+
} else if (ch === '$' && template[i + 1] === '(') {
|
|
234
|
+
frames.push({ kind: 'subst', parens: 0 });
|
|
235
|
+
out += '$(';
|
|
236
|
+
i++;
|
|
237
|
+
continue;
|
|
238
|
+
} else if (frame.kind === 'subst' && ch === '(') {
|
|
239
|
+
frame.parens++;
|
|
240
|
+
} else if (frame.kind === 'subst' && ch === ')') {
|
|
241
|
+
if (frame.parens === 0) frames.pop();
|
|
242
|
+
else frame.parens--;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const placeholder = placeholderAt(template, i);
|
|
246
|
+
if (placeholder) {
|
|
247
|
+
const name = reference(placeholder.key);
|
|
248
|
+
if (frame.kind === 'single') out += `'"\${${name}}"'`;
|
|
249
|
+
else if (frame.kind === 'double') out += `\${${name}}`;
|
|
250
|
+
else out += `"\${${name}}"`;
|
|
251
|
+
i += placeholder.length - 1;
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
out += ch;
|
|
75
255
|
}
|
|
76
256
|
|
|
77
|
-
|
|
78
|
-
|
|
257
|
+
return { command: out, values };
|
|
258
|
+
}
|
|
79
259
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
260
|
+
/**
|
|
261
|
+
* The cmd.exe counterpart, for local hooks on Windows. cmd expands %VAR%
|
|
262
|
+
* before it parses a line, so a variable would not protect anything there.
|
|
263
|
+
* Values are spliced in with every character cmd treats specially removed,
|
|
264
|
+
* and wrapped in double quotes when the placeholder sits outside them.
|
|
265
|
+
* @param {string} template
|
|
266
|
+
* @param {Record<string, any>} context
|
|
267
|
+
* @returns {string}
|
|
268
|
+
*/
|
|
269
|
+
export function renderCmdCommand(template, context = {}) {
|
|
270
|
+
let out = '';
|
|
271
|
+
let quoted = false;
|
|
272
|
+
for (let i = 0; i < template.length; i++) {
|
|
273
|
+
const ch = template[i];
|
|
274
|
+
if (ch === '"') quoted = !quoted;
|
|
275
|
+
const match = ch === '{' && PLACEHOLDER.exec(template.slice(i));
|
|
276
|
+
if (match && Object.hasOwn(context, match[1]) && isScalar(context[match[1]])) {
|
|
277
|
+
const value = String(context[match[1]]).replace(/["%!^&|<>()\r\n]/g, '_');
|
|
278
|
+
out += quoted ? value : `"${value}"`;
|
|
279
|
+
i += match[0].length - 1;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
out += ch;
|
|
83
283
|
}
|
|
284
|
+
return out;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Plain-text substitution for `log` messages, which never reach a shell.
|
|
289
|
+
* @param {string} template
|
|
290
|
+
* @param {Record<string, any>} context
|
|
291
|
+
*/
|
|
292
|
+
function renderText(template, context) {
|
|
293
|
+
return String(template).replace(/\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (whole, key) =>
|
|
294
|
+
Object.hasOwn(context, key) && isScalar(context[key]) ? String(context[key]) : whole);
|
|
295
|
+
}
|
|
84
296
|
|
|
297
|
+
/**
|
|
298
|
+
* Append one line to a hook log. A relative file name lands in the manager
|
|
299
|
+
* home, never in the working directory, which under an MCP client is whatever
|
|
300
|
+
* project the user has open. The home is not created just for a log: without
|
|
301
|
+
* it, the line still reaches stderr through the hook's own reporting.
|
|
302
|
+
* @param {string} file
|
|
303
|
+
* @param {string} message
|
|
304
|
+
* @returns {string|null} The file written, or null when skipped
|
|
305
|
+
*/
|
|
306
|
+
function appendHookLog(file, message) {
|
|
307
|
+
const home = managerHome();
|
|
308
|
+
if (!path.isAbsolute(file) && !fs.existsSync(home)) return null;
|
|
309
|
+
const target = path.resolve(home, file);
|
|
310
|
+
// One entry per line: a newline from a server must not forge a second one,
|
|
311
|
+
// and no other control character reaches the file either.
|
|
312
|
+
const text = Array.from(message.replace(/[\r\n]+/g, ' '),
|
|
313
|
+
c => (c < ' ' && c !== '\t') || c === '\u007f' ? '' : c).join('');
|
|
314
|
+
fs.appendFileSync(target, `[${new Date().toISOString()}] ${text}\n`, { mode: 0o600 });
|
|
315
|
+
return target;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Initialize hooks. Nothing is written: defaults live in memory, and the
|
|
320
|
+
* settings file appears only when a hook is changed.
|
|
321
|
+
*/
|
|
322
|
+
export async function initializeHooks() {
|
|
85
323
|
return true;
|
|
86
324
|
}
|
|
87
325
|
|
|
326
|
+
/**
|
|
327
|
+
* Read the user's hook settings, or null when there are none
|
|
328
|
+
*/
|
|
329
|
+
function readHooksFile() {
|
|
330
|
+
const state = readUserState(HOOKS_CONFIG_NAME, LEGACY_HOOKS_CONFIG_FILE);
|
|
331
|
+
return state ? JSON.parse(state.text) : null;
|
|
332
|
+
}
|
|
333
|
+
|
|
88
334
|
/**
|
|
89
335
|
* Load hooks configuration
|
|
90
336
|
*/
|
|
91
337
|
export function loadHooksConfig() {
|
|
338
|
+
// Copies, so toggling a hook never mutates the defaults held in memory.
|
|
339
|
+
const hooks = structuredClone({ ...DEFAULT_HOOKS, ...profileHooks });
|
|
92
340
|
try {
|
|
93
|
-
|
|
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
|
-
}
|
|
341
|
+
const customHooks = readHooksFile() || {};
|
|
115
342
|
|
|
116
|
-
|
|
343
|
+
// Deep merge hooks
|
|
344
|
+
for (const [hookName, hookConfig] of Object.entries(customHooks)) {
|
|
345
|
+
hooks[hookName] = hooks[hookName] ? { ...hooks[hookName], ...hookConfig } : hookConfig;
|
|
346
|
+
}
|
|
117
347
|
} catch (error) {
|
|
118
348
|
console.error(`Error loading hooks config: ${error.message}`);
|
|
119
349
|
}
|
|
120
|
-
return
|
|
350
|
+
return hooks;
|
|
121
351
|
}
|
|
122
352
|
|
|
123
353
|
/**
|
|
@@ -125,7 +355,7 @@ export function loadHooksConfig() {
|
|
|
125
355
|
*/
|
|
126
356
|
function saveHooksConfig(config) {
|
|
127
357
|
try {
|
|
128
|
-
|
|
358
|
+
writeUserState(HOOKS_CONFIG_NAME, `${JSON.stringify(config, null, 2)}\n`);
|
|
129
359
|
return true;
|
|
130
360
|
} catch (error) {
|
|
131
361
|
console.error(`Error saving hooks config: ${error.message}`);
|
|
@@ -147,7 +377,7 @@ export async function executeHook(hookName, context = {}) {
|
|
|
147
377
|
console.error(`🎣 Executing hook: ${hookName}`);
|
|
148
378
|
const results = [];
|
|
149
379
|
|
|
150
|
-
for (const action of hook.actions) {
|
|
380
|
+
for (const action of hook.actions || []) {
|
|
151
381
|
try {
|
|
152
382
|
// Check environment variables if required
|
|
153
383
|
if (action.requiresEnv) {
|
|
@@ -161,14 +391,8 @@ export async function executeHook(hookName, context = {}) {
|
|
|
161
391
|
}
|
|
162
392
|
}
|
|
163
393
|
|
|
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
394
|
// Execute action based on type
|
|
171
|
-
const result = await executeAction(action,
|
|
395
|
+
const result = await executeAction(action, context);
|
|
172
396
|
results.push(result);
|
|
173
397
|
|
|
174
398
|
// Check validation results
|
|
@@ -204,7 +428,7 @@ export async function executeHook(hookName, context = {}) {
|
|
|
204
428
|
/**
|
|
205
429
|
* Execute a single action
|
|
206
430
|
*/
|
|
207
|
-
async function executeAction(action,
|
|
431
|
+
async function executeAction(action, context) {
|
|
208
432
|
const result = {
|
|
209
433
|
action: action.name,
|
|
210
434
|
type: action.type,
|
|
@@ -212,9 +436,17 @@ async function executeAction(action, command, context) {
|
|
|
212
436
|
};
|
|
213
437
|
|
|
214
438
|
try {
|
|
215
|
-
if (action.
|
|
216
|
-
|
|
217
|
-
|
|
439
|
+
if (action.type === 'log') {
|
|
440
|
+
const file = appendHookLog(action.file || 'hooks.log', renderText(action.message || '', context));
|
|
441
|
+
result.success = true;
|
|
442
|
+
result.output = file ? `logged to ${file}` : 'skipped: no manager home to write the log in';
|
|
443
|
+
} else if (action.remoteCommand && context.sshConnection) {
|
|
444
|
+
// The variables are set in the remote shell itself: an SSH exec request
|
|
445
|
+
// cannot rely on the server accepting environment variables.
|
|
446
|
+
const { command, values } = renderShellCommand(action.remoteCommand, context);
|
|
447
|
+
const assignments = Object.entries(values).map(([name, value]) => `${name}=${shellQuote(value)}; `).join('');
|
|
448
|
+
const remote = assignments ? `{ ${assignments}${command}\n}` : command;
|
|
449
|
+
const output = await context.sshConnection.execCommand(remote, {
|
|
218
450
|
cwd: context.cwd || context.defaultDir
|
|
219
451
|
});
|
|
220
452
|
|
|
@@ -227,7 +459,16 @@ async function executeAction(action, command, context) {
|
|
|
227
459
|
}
|
|
228
460
|
} else if (action.command) {
|
|
229
461
|
// Execute locally
|
|
230
|
-
|
|
462
|
+
let command;
|
|
463
|
+
let env = process.env;
|
|
464
|
+
if (process.platform === 'win32') {
|
|
465
|
+
command = renderCmdCommand(action.command, context);
|
|
466
|
+
} else {
|
|
467
|
+
const rendered = renderShellCommand(action.command, context);
|
|
468
|
+
command = rendered.command;
|
|
469
|
+
env = { ...process.env, ...rendered.values };
|
|
470
|
+
}
|
|
471
|
+
const { stdout, stderr } = await execAsync(command, { env });
|
|
231
472
|
|
|
232
473
|
result.output = stdout;
|
|
233
474
|
result.error = stderr;
|
|
@@ -243,7 +484,7 @@ async function executeAction(action, command, context) {
|
|
|
243
484
|
case 'backup':
|
|
244
485
|
result.backupInfo = {
|
|
245
486
|
timestamp: new Date().toISOString(),
|
|
246
|
-
command: command
|
|
487
|
+
command: action.command || action.remoteCommand
|
|
247
488
|
};
|
|
248
489
|
break;
|
|
249
490
|
|