-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlocalstack-extensions.ts
More file actions
313 lines (282 loc) · 10.3 KB
/
Copy pathlocalstack-extensions.ts
File metadata and controls
313 lines (282 loc) · 10.3 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
import { z } from "zod";
import { type ToolMetadata, type InferSchema } from "xmcp";
import {
runPreflights,
requireLocalStackRunning,
requireProFeature,
requireAuthToken,
requireDockerDaemon,
} from "../core/preflight";
import { ResponseBuilder } from "../core/response-builder";
import { ProFeature } from "../lib/localstack/license-checker";
import { withToolAnalytics } from "../core/analytics";
import { DockerApiClient } from "../lib/docker/docker.client";
import {
recreateRunningContainer,
restartRuntimeInPlace,
} from "../lib/localstack/localstack.utils";
import {
EXTENSIONS_MANAGER_COMMAND,
EXTENSIONS_VENV_REPAIR_SCRIPT,
formatInstalledExtensions,
parseExtensionEvents,
parseInstalledExtensions,
summarizeInstall,
summarizeUninstall,
validateExtensionTarget,
type ExtensionOutcome,
} from "../lib/localstack/extensions.logic";
import { PlatformApiClient, describePlatformError } from "../lib/localstack/platform.client";
const EXTENSION_EXEC_TIMEOUT_MS = 120000;
export const schema = {
action: z
.enum(["list", "install", "uninstall", "available"])
.describe(
"list = installed extensions; install = install an extension; uninstall = remove an extension; available = browse the marketplace/extensions library"
),
name: z
.string()
.optional()
.describe(
"Extension package name (e.g. 'localstack-extension-typedb' or 'localstack-extension-typedb==1.0.0'). Required for install and uninstall actions."
),
source: z
.string()
.optional()
.describe(
"Git URL to install from (e.g. 'git+https://github.com/org/repo.git'). Use this instead of name when installing from a repository."
),
};
export const metadata: ToolMetadata = {
name: "localstack-extensions",
description: "Install, uninstall, list, and discover LocalStack Extensions from the marketplace",
annotations: {
title: "LocalStack Extensions",
readOnlyHint: false,
destructiveHint: false,
idempotentHint: false,
},
};
export default async function localstackExtensions({
action,
name,
source,
}: InferSchema<typeof schema>) {
return withToolAnalytics("localstack-extensions", { action, name, source }, async () => {
// `available` only needs the platform API; the container-touching actions need a
// running LocalStack (the extension manager runs inside it) + the Docker daemon.
const checks =
action === "available"
? [requireAuthToken()]
: [
requireAuthToken(),
requireDockerDaemon(),
requireLocalStackRunning(),
requireProFeature(ProFeature.EXTENSIONS),
];
const preflightError = await runPreflights(checks);
if (preflightError) return preflightError;
switch (action) {
case "list":
return await handleList();
case "install":
return await handleInstall(name, source);
case "uninstall":
return await handleUninstall(name);
case "available":
return await handleAvailable();
default:
return ResponseBuilder.error("Unknown action", `Unsupported action: ${action}`);
}
});
}
/**
* Run the extension manager module inside the running LocalStack container. The
* manager pip-installs into the extensions venv on /var/lib/localstack.
*/
async function runExtensionManager(args: string[], { repairVenv = false } = {}) {
const docker = new DockerApiClient();
const containerId = await docker.findLocalStackContainer();
if (repairVenv) {
const repair = await docker.executeInContainer(
containerId,
["sh", "-c", EXTENSIONS_VENV_REPAIR_SCRIPT],
undefined,
{
env: ["DEBUG=0"],
timeoutMs: EXTENSION_EXEC_TIMEOUT_MS,
}
);
if (repair.exitCode !== 0) {
// e.g. a custom image where the extension manager's interpreter lives at a
// different path — fail with the real cause instead of letting the manager
// produce an unrelated-looking error later.
const detail = repair.stderr || repair.stdout || `exit code ${repair.exitCode}`;
throw new Error(`the extensions environment could not be prepared: ${detail}`);
}
}
return await docker.executeInContainer(
containerId,
[...EXTENSIONS_MANAGER_COMMAND, ...args],
undefined,
{ env: ["DEBUG=0"], timeoutMs: EXTENSION_EXEC_TIMEOUT_MS }
);
}
function outcomeSummaryBlock(outcome: ExtensionOutcome): string {
return outcome.summaryLines.length
? `\n\n\`\`\`\n${outcome.summaryLines.join("\n")}\n\`\`\``
: "";
}
/**
* Activate an install/uninstall by restarting the runtime. Prefer the fast in-place
* restart; if it doesn't confirm (the in-place path can leave the runtime down under
* heavy Lambda load), fall back to a full container recreate — which is reliable — so
* the caller never ends up with a silently-dead stack.
*/
async function activationSuffix(): Promise<string> {
const restart = await restartRuntimeInPlace();
if (restart.ok) {
return "\n\nLocalStack was restarted to activate the change. ✅";
}
const recreate = await recreateRunningContainer();
const recreatedOk = !recreate.content[0]?.text?.trimStart().startsWith("❌");
if (recreatedOk) {
return "\n\nThe in-place restart did not confirm, so LocalStack was recreated to activate the change. ✅";
}
return (
`\n\n⚠️ Could not confirm LocalStack restarted to activate the change (${restart.detail}) ` +
"and the recreate fallback also did not confirm. Check LocalStack status and restart it with the management tool."
);
}
async function handleList() {
let result;
try {
result = await runExtensionManager(["list"]);
} catch (error) {
return ResponseBuilder.error(
"List Failed",
`Could not run the extension manager in the LocalStack container: ${
error instanceof Error ? error.message : String(error)
}`
);
}
const extensions = parseInstalledExtensions(result.stdout);
if (extensions.length === 0 && result.exitCode !== 0) {
return ResponseBuilder.error(
"List Failed",
result.stderr || result.stdout || "Failed to list installed extensions."
);
}
return ResponseBuilder.markdown(formatInstalledExtensions(extensions));
}
async function handleInstall(name?: string, source?: string) {
const hasName = !!name;
const hasSource = !!source;
if ((hasName && hasSource) || (!hasName && !hasSource)) {
return ResponseBuilder.error(
"Invalid Parameters",
"Provide either `name` or `source` for install, but not both."
);
}
const validationError = validateExtensionTarget({ name, source });
if (validationError) {
return ResponseBuilder.error("Invalid Extension Target", validationError);
}
const target = source || name!;
let result;
try {
result = await runExtensionManager(["install", target], { repairVenv: true });
} catch (error) {
return ResponseBuilder.error(
"Install Failed",
`Could not run the extension manager in the LocalStack container: ${
error instanceof Error ? error.message : String(error)
}`
);
}
const outcome = summarizeInstall(parseExtensionEvents(result.stdout));
if (outcome.kind === "not-found") {
return ResponseBuilder.error(
"Extension Not Found",
`Could not resolve the extension package '${target}'. Please verify it exists on PyPI, or provide a git repository URL using the source parameter.`
);
}
if (!outcome.success) {
return ResponseBuilder.error(
"Install Failed",
`${outcome.errorDetail || "Extension installation failed."}${outcomeSummaryBlock(outcome)}`
);
}
if (outcome.kind === "already-installed") {
return ResponseBuilder.markdown(
`## Extension Installation Result${outcomeSummaryBlock(outcome)}\n\nThe extension is already installed — no restart needed.`
);
}
const restartNote = await activationSuffix();
return ResponseBuilder.markdown(
`## Extension Installation Result${outcomeSummaryBlock(outcome)}${restartNote}`
);
}
async function handleUninstall(name?: string) {
if (!name) {
return ResponseBuilder.error(
"Missing Required Parameter",
"The `uninstall` action requires the `name` parameter to be specified."
);
}
const validationError = validateExtensionTarget({ name });
if (validationError) {
return ResponseBuilder.error("Invalid Extension Target", validationError);
}
let result;
try {
result = await runExtensionManager(["uninstall", name], { repairVenv: true });
} catch (error) {
return ResponseBuilder.error(
"Uninstall Failed",
`Could not run the extension manager in the LocalStack container: ${
error instanceof Error ? error.message : String(error)
}`
);
}
const outcome = summarizeUninstall(parseExtensionEvents(result.stdout));
if (outcome.kind === "not-installed") {
return ResponseBuilder.error(
"Extension Not Installed",
outcome.errorDetail || `Extension '${name}' is not installed.`
);
}
if (!outcome.success) {
return ResponseBuilder.error(
"Uninstall Failed",
`${outcome.errorDetail || "Extension uninstallation failed."}${outcomeSummaryBlock(outcome)}`
);
}
const restartNote = await activationSuffix();
return ResponseBuilder.markdown(
`## Extension Uninstall Result${outcomeSummaryBlock(outcome)}${restartNote}`
);
}
async function handleAvailable() {
const token = process.env.LOCALSTACK_AUTH_TOKEN!;
const client = new PlatformApiClient(token);
try {
const marketplace = await client.getExtensionsMarketplace();
const simplified = marketplace.map((item) => ({
name: item.name || "unknown-extension",
summary: item.summary || item.description || "No summary provided.",
author: item.author || "Unknown",
version: item.version || "Unknown",
}));
let markdown = `# LocalStack Extensions Marketplace\n\n${simplified.length} extensions available. Install any with the \`install\` action.\n\n---`;
for (const extension of simplified) {
markdown += `\n\n### ${extension.name}\n**Author:** ${extension.author} | **Version:** ${extension.version}\n${extension.summary}\n\n---`;
}
return ResponseBuilder.markdown(markdown);
} catch (error) {
return ResponseBuilder.error(
"Marketplace Fetch Failed",
describePlatformError(error, "the extensions marketplace")
);
}
}