-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.ts
More file actions
91 lines (83 loc) · 2.56 KB
/
setup.ts
File metadata and controls
91 lines (83 loc) · 2.56 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
import type { CancellationToken, LogOutputChannel } from "vscode";
import * as z from "zod/v4-mini";
import { LOCALSTACK_DOCKER_IMAGE_NAME } from "../constants.ts";
import { checkIsAuthenticated } from "./authenticate.ts";
import { checkIsProfileConfigured } from "./configure-aws.ts";
import { exec } from "./exec.ts";
import { checkLocalstackInstalled } from "./install.ts";
import { checkIsLicenseValid } from "./license.ts";
import { spawn } from "./spawn.ts";
export async function checkSetupStatus(outputChannel: LogOutputChannel) {
const [isInstalled, isAuthenticated, isLicenseValid, isProfileConfigured] =
await Promise.all([
checkLocalstackInstalled(outputChannel),
checkIsAuthenticated(),
checkIsLicenseValid(outputChannel),
checkIsProfileConfigured(),
]);
return {
isInstalled,
isAuthenticated,
isLicenseValid,
isProfileConfigured,
};
}
export async function updateDockerImage(
outputChannel: LogOutputChannel,
cancellationToken: CancellationToken,
): Promise<void> {
const imageVersion = await getDockerImageSemverVersion(outputChannel);
if (!imageVersion) {
await pullDockerImage(outputChannel, cancellationToken);
}
}
const InspectSchema = z.array(
z.object({
Config: z.object({
Env: z.array(z.string()),
}),
}),
);
async function getDockerImageSemverVersion(
outputChannel: LogOutputChannel,
): Promise<string | undefined> {
try {
const { stdout } = await exec(
`docker inspect ${LOCALSTACK_DOCKER_IMAGE_NAME}`,
);
const data: unknown = JSON.parse(stdout);
const parsed = InspectSchema.safeParse(data);
if (!parsed.success) {
throw new Error(
`Could not parse "docker inspect" output: ${JSON.stringify(z.treeifyError(parsed.error))}`,
);
}
const env = parsed.data[0]?.Config.Env ?? [];
const imageVersion = env
.find((line) => line.startsWith("LOCALSTACK_BUILD_VERSION="))
?.slice("LOCALSTACK_BUILD_VERSION=".length);
if (!imageVersion) {
return;
}
return imageVersion;
} catch (error) {
outputChannel.error("Could not inspect LocalStack docker image");
outputChannel.error(error instanceof Error ? error : String(error));
return undefined;
}
}
async function pullDockerImage(
outputChannel: LogOutputChannel,
cancellationToken: CancellationToken,
): Promise<void> {
try {
await spawn("docker", ["pull", LOCALSTACK_DOCKER_IMAGE_NAME], {
outputChannel,
outputLabel: "docker.pull",
cancellationToken: cancellationToken,
});
} catch (error) {
outputChannel.error("Could not pull LocalStack docker image");
outputChannel.error(error instanceof Error ? error : String(error));
}
}