-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.ts
More file actions
642 lines (572 loc) · 18.2 KB
/
install.ts
File metadata and controls
642 lines (572 loc) · 18.2 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
import { mkdir, open, rm, symlink, appendFile } from "node:fs/promises";
import os, { homedir } from "node:os";
import path, { join } from "node:path";
import { move } from "fs-extra/esm";
import { window } from "vscode";
import type { CancellationToken, LogOutputChannel, Progress } from "vscode";
import * as z from "zod/v4-mini";
import {
GLOBAL_CLI_INSTALLATION_DIRNAME,
LOCAL_CLI_INSTALLATION_DIRNAME,
} from "../constants.ts";
import { execLocalStack } from "./cli.ts";
import { exec } from "./exec.ts";
import { minDelay } from "./promises.ts";
import {
spawnElevatedDarwin,
spawnElevatedLinux,
spawnElevatedWindows,
} from "./prompts.ts";
import { spawn } from "./spawn.ts";
import type { Telemetry } from "./telemetry.ts";
export async function checkLocalstackInstalled(
outputChannel: LogOutputChannel,
): Promise<boolean> {
try {
await execLocalStack(["--version"], { outputChannel });
return true;
} catch (error) {
return false;
}
}
export async function runInstallProcess(
progress: Progress<{ message: string }>,
cancellationToken: CancellationToken,
outputChannel: LogOutputChannel,
telemetry: Telemetry,
origin?: "extension_startup" | "manual_trigger",
): Promise<{ cancelled: boolean; skipped?: boolean }> {
/////////////////////////////////////////////////////////////////////
const origin_trigger = origin ? origin : "manual_trigger";
progress.report({
message: "Verifying CLI installation...",
});
const startedAt = new Date().toISOString();
const isLocalStackInstalled = await minDelay(
checkLocalstackInstalled(outputChannel),
);
if (cancellationToken.isCancellationRequested) {
return { cancelled: true };
}
/////////////////////////////////////////////////////////////////////
if (isLocalStackInstalled) {
progress.report({
message: "Skipping CLI installation...",
});
telemetry.track({
name: "emulator_installed",
payload: {
namespace: "onboarding",
origin: origin_trigger,
step_order: 1,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "SKIPPED",
},
});
await minDelay();
return { cancelled: false, skipped: true };
}
/////////////////////////////////////////////////////////////////////
progress.report({ message: "Downloading latest CLI release..." });
const downloadURL = await getCLIDownloadURL();
return await withTempDir<{ cancelled: boolean }>(async (temporaryDirname) => {
await minDelay(
downloadAndUnarchive(downloadURL, temporaryDirname, outputChannel),
);
const platform = os.platform();
/////////////////////////////////////////////////////////////////////
progress.report({
message: "Choose installation scope...",
});
const installScope = await window.showInformationMessage(
"Choose a LocalStack CLI installation scope",
{
modal: true,
},
{
title: "Install for current user only",
value: "local",
},
{
title: "Install for all users",
value: "global",
},
);
if (!installScope) {
window.showErrorMessage("The installation was cancelled by the user");
return { cancelled: true };
}
if (installScope.value === "global") {
/////////////////////////////////////////////////////////////////////
progress.report({
message: "Waiting for elevated privileges...",
});
switch (platform) {
case "darwin":
await installGlobalDarwin(
progress,
cancellationToken,
outputChannel,
temporaryDirname,
);
break;
case "linux":
await installGlobalLinux(
progress,
cancellationToken,
outputChannel,
temporaryDirname,
);
break;
case "win32":
await installGlobalWindows(
progress,
cancellationToken,
outputChannel,
temporaryDirname,
);
break;
default:
telemetry.track({
name: "emulator_installed",
payload: {
namespace: "onboarding",
origin: origin_trigger,
step_order: 1,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "FAILED",
installation_scope: "GLOBAL",
errors: [`Unsupported platform: ${platform}`],
},
});
throw new Error(`Unsupported platform: ${platform}`);
}
} else if (installScope.value === "local") {
/////////////////////////////////////////////////////////////////////
progress.report({
message: "Installing LocalStack CLI for current user...",
});
switch (platform) {
case "darwin":
case "linux":
await installLocalDarwinLinux(temporaryDirname);
break;
case "win32":
await installLocalWindows(temporaryDirname);
break;
default:
telemetry.track({
name: "emulator_installed",
payload: {
namespace: "onboarding",
origin: origin_trigger,
step_order: 1,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "FAILED",
installation_scope: "LOCAL",
errors: [`Unsupported platform: ${platform}`],
},
});
throw new Error(`Unsupported platform: ${platform}`);
}
}
telemetry.track({
name: "emulator_installed",
payload: {
namespace: "onboarding",
origin: origin_trigger,
step_order: 1,
started_at: startedAt,
ended_at: new Date().toISOString(),
status: "COMPLETED",
installation_scope: installScope?.value,
},
});
return {
cancelled: false,
};
});
}
/**
* Zod schema for a single asset in a GitHub release.
*/
const AssetSchema = z.object({
name: z.string(),
browser_download_url: z.url(),
});
/**
* Zod schema for the GitHub release API response.
*/
const ReleaseSchema = z.object({
tag_name: z.string(),
assets: z.array(AssetSchema),
});
type ReleaseSchema = z.infer<typeof ReleaseSchema>;
/**
* Fetches the latest LocalStack CLI release and returns download links for each OS/arch.
* @throws If the fetch fails or the response is invalid.
*/
async function fetchLatestCLIRelease(): Promise<ReleaseSchema> {
const res = await fetch(
"https://api.github.com/repos/localstack/localstack-cli/releases/latest",
{
headers: {
Accept: "application/vnd.github+json",
},
},
);
if (!res.ok) {
throw new Error(`Failed to fetch release: ${res.status} ${res.statusText}`);
}
const data = await res.json();
const parsed = ReleaseSchema.safeParse(data);
if (!parsed.success) {
console.error(z.treeifyError(parsed.error));
throw new Error("Invalid GitHub release response");
}
return parsed.data;
}
/**
* Detects the host OS and architecture in a format matching LocalStack CLI asset names.
* @returns {string} The platform, e.g. "darwin-arm64", "linux-amd64", "windows-amd64"
*/
function detectPlatform(): string {
const platform = os.platform();
const arch = os.arch();
let osPart: string;
switch (platform) {
case "darwin":
osPart = "darwin";
break;
case "win32":
osPart = "windows";
break;
case "linux":
osPart = "linux";
break;
default:
throw new Error(`Unsupported platform: ${platform}`);
}
let archPart: string;
switch (arch) {
case "x64":
archPart = "amd64";
break;
case "arm64":
archPart = "arm64";
break;
default:
throw new Error(`Unsupported architecture: ${arch}`);
}
return `${osPart}-${archPart}`;
}
async function getCLIDownloadURL(): Promise<string> {
const platform = detectPlatform();
const release = await fetchLatestCLIRelease();
const version = release.tag_name.slice(1);
const asset = release.assets.find(
(asset) =>
asset.name === `localstack-cli-${version}-${platform}.tar.gz` ||
asset.name === `localstack-cli-${version}-${platform}.zip`,
);
const url = asset?.browser_download_url;
if (!url) {
throw new Error("Download URL not found");
}
return url;
}
/**
* Options for downloading a file.
*/
interface DownloadFileOptions {
/** The URL to download from. */
url: string;
/** The full destination file path to write to. */
destinationFilename: string;
}
/**
* Downloads a file from a URL to a specified destination path.
* @param options The download options.
* @returns The full path to the downloaded file.
* @throws If the download fails.
*/
async function downloadFile(options: DownloadFileOptions): Promise<string> {
const { url, destinationFilename: destinationPath } = options;
const res = await fetch(url);
if (!res.ok || !res.body) {
throw new Error(`Failed to download file: ${res.status} ${res.statusText}`);
}
// Create a writable stream to the destination path
const fileHandle = await open(destinationPath, "w");
try {
const writable = fileHandle.createWriteStream();
await new Promise<void>((resolve, reject) => {
const reader = res.body?.getReader();
function pump() {
reader
?.read()
.then(({ done, value }) => {
if (done) {
writable.end();
resolve();
return;
}
writable.write(value, pump);
})
.catch(reject);
}
writable.on("error", reject);
pump();
});
} finally {
await fileHandle.close();
}
return destinationPath;
}
async function downloadAndUnarchive(
url: string,
destinationDirname: string,
outputChannel: LogOutputChannel,
) {
const extension = url.endsWith(".tar.gz") ? "tar.gz" : "zip";
const archiveFilename = `${destinationDirname}/cli.${extension}`;
await minDelay(async () => {
await downloadFile({
url,
destinationFilename: archiveFilename,
});
await unarchive(archiveFilename, destinationDirname, outputChannel);
});
}
/**
* Extracts a .tar.gz or .zip archive to the specified destination directory.
* @param archiveFilename The path to the archive file.
* @param destinationDirname The directory to extract to.
* @returns The destination directory.
* @throws If the file type is unsupported or extraction fails.
*/
async function unarchive(
archiveFilename: string,
destinationDirname: string,
outputChannel: LogOutputChannel,
): Promise<string> {
await mkdir(destinationDirname, { recursive: true });
await spawn("tar", ["-xzf", archiveFilename, "-C", destinationDirname], {
outputLabel: "tar",
outputChannel,
});
return destinationDirname;
}
/**
* Creates a temporary directory, runs a callback, and ensures cleanup.
* @param callback Function that receives the temp dir path.
*/
async function withTempDir<T>(
callback: (tmpPath: string) => Promise<T>,
): Promise<T> {
const tempDirPath = join(
homedir(),
".localstack",
`localstack-temp-install-${Math.random().toString(36).substring(0, 5)}`,
);
await mkdir(tempDirPath, { recursive: true });
try {
return await callback(tempDirPath);
} finally {
// Remove the directory and its contents recursively
await rm(tempDirPath, { recursive: true, force: true });
}
}
async function ensureLocalBinInPath() {
try {
const homeDir = os.homedir();
const dotLocalstackDir = ".localstack";
const profileSetupScriptName = "localstack_setup.sh";
const setupScriptPath = path.join(
homeDir,
dotLocalstackDir,
profileSetupScriptName,
);
const setupScriptContent = `
if [[ ":$PATH:" != *":$HOME/.local/bin:"* ]]; then
export PATH="$HOME/.local/bin:$PATH"
fi
`.trim();
await mkdir(path.join(homeDir, ".localstack"), { recursive: true });
const fh = await open(setupScriptPath, "w");
await fh.writeFile(setupScriptContent);
await fh.close();
const shellPath = process.env.SHELL || "";
// biome-ignore format: auto-formatting makes the ternary unreadable
const profileFile =
shellPath.endsWith("zsh") ? path.join(homeDir, ".zshrc") :
shellPath.endsWith("bash") ? path.join(homeDir, ".bashrc") :
"";
const sourceLocalstackInShellProfile = `source $HOME/${dotLocalstackDir}/${profileSetupScriptName}`;
if (profileFile) {
const profileContent = await open(profileFile, "r").then(async (fh) => {
try {
return await fh.readFile({ encoding: "utf8" }); // will throw if file doesn't exist, in this case we just show info message and don't create any profile files as that would be too intrusive
} finally {
await fh.close();
}
});
if (!profileContent.includes(sourceLocalstackInShellProfile)) {
await appendFile(
profileFile,
`\n# Added by LocalStack installer\n${sourceLocalstackInShellProfile}\n`,
);
window.showInformationMessage(
`Updated your shell profile (${profileFile}) to include ~/.local/bin in PATH. Restart your terminal to apply changes.`,
);
}
} else {
window.showInformationMessage(
`Could not detect your shell profile. To use localstack CLI from terminal ensure ~/.local/bin is in your PATH.`,
);
}
// Update PATH for the current VSCode process so LocalStack is immediately available
const localBinPath = path.join(homeDir, ".local", "bin");
const currentPath = process.env.PATH || "";
if (!currentPath.split(":").includes(localBinPath)) {
process.env.PATH = `${localBinPath}:${currentPath}`;
}
} catch (err) {
window.showInformationMessage(
`Could not update your shell profile. To use localstack CLI from terminal ensure ~/.local/bin is in your PATH.`,
);
}
}
async function installLocalDarwinLinux(temporaryDirname: string) {
const homeDir = os.homedir();
const localstackBinary = `${LOCAL_CLI_INSTALLATION_DIRNAME}/localstack`;
const localBinDir = `${homeDir}/.local/bin`;
const symlinkToBinary = `${localBinDir}/localstack`;
// Ensure ~/.local/bin exists
await mkdir(localBinDir, { recursive: true });
// Remove old files if present
await rm(symlinkToBinary, { force: true }).catch(() => {});
await rm(LOCAL_CLI_INSTALLATION_DIRNAME, {
recursive: true,
force: true,
}).catch(() => {});
// Move binary to ~/.local/localstack/localstack
// using move from fs-extra for better compatibility with linux as temp dir and user home can be on different filesystems
await move(`${temporaryDirname}/localstack`, LOCAL_CLI_INSTALLATION_DIRNAME);
// Create symlink ~/.local/bin/localstack -> ~/.local/localstack/localstack
await symlink(localstackBinary, symlinkToBinary);
await ensureLocalBinInPath();
window.showInformationMessage("LocalStack CLI installed for current user.");
}
async function installLocalWindows(temporaryDirname: string) {
await rm(LOCAL_CLI_INSTALLATION_DIRNAME, { recursive: true, force: true });
await move(`${temporaryDirname}/localstack`, LOCAL_CLI_INSTALLATION_DIRNAME);
await exec(`setx PATH "%PATH%;${LOCAL_CLI_INSTALLATION_DIRNAME}"`);
window.showInformationMessage("LocalStack CLI installed for current user.");
}
async function installGlobalDarwin(
progress: Progress<{ message: string }>,
cancellationToken: CancellationToken,
outputChannel: LogOutputChannel,
temporaryDirname: string,
) {
// Use elevated privileges to install globally on macOS
const { cancelled } = await spawnElevatedDarwin({
//TODO consider loading script from a file
script: `rm -rf /usr/local/localstack && mv ${temporaryDirname}/localstack /usr/local/localstack && ln -sf /usr/local/localstack/localstack /usr/local/bin/localstack`,
outputChannel,
outputLabel: "install",
cancellationToken,
});
if (cancelled) {
//TODO check if progress can be used instead of window
window.showErrorMessage("The installation was cancelled by the user");
return { cancelled: true };
}
}
async function installGlobalLinux(
progress: Progress<{ message: string }>,
cancellationToken: CancellationToken,
outputChannel: LogOutputChannel,
temporaryDirname: string,
) {
// Use elevated privileges to install globally on linux
try {
const { cancelled } = await spawnElevatedLinux({
//TODO consider loading script from a file
script: `rm -rf /usr/local/localstack && mv ${temporaryDirname}/localstack /usr/local/localstack && ln -sf /usr/local/localstack/localstack /usr/local/bin/localstack`,
outputChannel,
outputLabel: "install",
cancellationToken,
});
if (cancelled) {
//TODO check if progress can be used instead of window
window.showErrorMessage("The installation was cancelled by the user");
return { cancelled: true };
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
window.showErrorMessage(`Installation failed: ${message}`);
throw error;
}
}
async function installGlobalWindows(
progress: Progress<{ message: string }>,
cancellationToken: CancellationToken,
outputChannel: LogOutputChannel,
temporaryDirName: string,
) {
const localstackExecutableDir = path.join(
GLOBAL_CLI_INSTALLATION_DIRNAME,
"localstack",
);
// Run installation commands with elevated permissions on Windows
// TODO remove extra verbosity added during debug
const installScript = `
Write-Host "=== INSTALLATION STARTED ==="
$localstackPath = '${GLOBAL_CLI_INSTALLATION_DIRNAME}';
$localstackExecutableDir = '${localstackExecutableDir}';
Write-Host "Source temp dir: '${temporaryDirName}'"
Write-Host "Target path: $localstackPath"
Write-Host "Localstack executable dir: $localstackExecutableDir"
# 1. Remove directory if it exists
if (Test-Path $localstackPath) {
Write-Host "Removing existing directory..."
Remove-Item -LiteralPath $localstackPath -Recurse -Force -ErrorAction Stop
} else {
Write-Host "No existing directory to remove."
}
# 2. Move directory from temp to target
if (Test-Path '${temporaryDirName}') {
Write-Host "Moving '${temporaryDirName}' to $localstackPath..."
Move-Item -LiteralPath '${temporaryDirName}' -Destination $localstackPath -ErrorAction Stop
} else {
Write-Host "ERROR: Temp directory not found - cannot move." -ForegroundColor Red
exit 1
}
# 3. Add to PATH if missing
$currentPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine)
Write-Host "Current PATH: $currentPath"
if (-not ($currentPath.Split(';') -contains $localstackExecutableDir)) {
Write-Host "Updating system PATH..."
$newPath = "\${currentPath};\${localstackExecutableDir}"
[Environment]::SetEnvironmentVariable('Path', $newPath, [EnvironmentVariableTarget]::Machine)
Write-Host "PATH updated."
} else {
Write-Host "Path already contains $localstackExecutableDir"
}
Write-Host "=== INSTALLATION COMPLETED SUCCESSFULLY ==="
`;
await spawnElevatedWindows({
script: installScript,
outputChannel,
outputLabel: "install",
cancellationToken,
});
window.showInformationMessage(
"LocalStack CLI installed globally for all users.",
);
}