Switch to node, give up on bun
This commit is contained in:
@@ -7,34 +7,38 @@ import type { JSX } from "react";
|
||||
function DockerRow({
|
||||
containerInfo,
|
||||
}: {
|
||||
containerInfo: dockerRouterType['list'][number]
|
||||
containerInfo: dockerRouterType["list"][number];
|
||||
}) {
|
||||
const { data: latest, isError, isLoading } = api.docker.latest.useQuery({ id: containerInfo.container.id });
|
||||
const outdated = containerInfo.image.current.hash !== latest?.latest.hash;
|
||||
|
||||
let latestFragment: JSX.Element | null = null;
|
||||
if (isError) {
|
||||
latestFragment = (
|
||||
<>
|
||||
<td>{"Error"}</td>
|
||||
<td>{"Error"}</td>
|
||||
</>
|
||||
)
|
||||
} else if (isLoading) {
|
||||
latestFragment = (
|
||||
<>
|
||||
<td><span className="loading loading-dots loading-lg"></span></td>
|
||||
<td><span className="loading loading-dots loading-lg"></span></td>
|
||||
</>
|
||||
)
|
||||
} else if (latest) {
|
||||
latestFragment = (
|
||||
<>
|
||||
<td>{latest?.latest.tag}</td>
|
||||
<td>{latest?.latest.hash}</td>
|
||||
</>
|
||||
)
|
||||
}
|
||||
let latestFragment: JSX.Element | null = null;
|
||||
if (isError) {
|
||||
latestFragment = (
|
||||
<>
|
||||
<td>{"Error"}</td>
|
||||
<td>{"Error"}</td>
|
||||
</>
|
||||
);
|
||||
} else if (isLoading) {
|
||||
latestFragment = (
|
||||
<>
|
||||
<td>
|
||||
<span className="loading loading-dots loading-lg" />
|
||||
</td>
|
||||
<td>
|
||||
<span className="loading loading-dots loading-lg" />
|
||||
</td>
|
||||
</>
|
||||
);
|
||||
} else if (latest) {
|
||||
latestFragment = (
|
||||
<>
|
||||
<td>{latest?.latest.tag}</td>
|
||||
<td>{latest?.latest.hash}</td>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<tr key={containerInfo.container.name} className={`${outdated ? "bg-base-200" : null}`}>
|
||||
@@ -42,7 +46,7 @@ function DockerRow({
|
||||
<td>{containerInfo.image.name}</td>
|
||||
<td>{containerInfo.image.current.tag}</td>
|
||||
<td>{containerInfo.image.current.hash}</td>
|
||||
{latestFragment}
|
||||
{latestFragment}
|
||||
</tr>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,195 +8,197 @@ import { createTRPCRouter, publicProcedure } from "@/server/api/trpc";
|
||||
import { TRPCError, type inferRouterOutputs } from "@trpc/server";
|
||||
|
||||
export const dockerRouter = createTRPCRouter({
|
||||
latest: publicProcedure.input(z.object({ id: z.string() })).query(async ({ input }) => {
|
||||
const docker = getDocker();
|
||||
const containers = await getContainers(docker);
|
||||
latest: publicProcedure.input(z.object({ id: z.string() })).query(async ({ input }) => {
|
||||
const docker = getDocker();
|
||||
const containers = await getContainers(docker);
|
||||
|
||||
const container = containers.find((container) => container.Id === input.id);
|
||||
if (!container) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: `Container with id ${input.id} not found`,
|
||||
});
|
||||
}
|
||||
const container = containers.find((container) => container.Id === input.id);
|
||||
if (!container) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: `Container with id ${input.id} not found`,
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const imageData = await getImageData(docker, container);
|
||||
try {
|
||||
const imageData = await getImageData(docker, container);
|
||||
|
||||
return {
|
||||
latest: await getLatest(imageData.name, imageData.tag),
|
||||
};
|
||||
} catch (ex) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: (ex as Error).message,
|
||||
cause: (ex as Error).cause,
|
||||
});
|
||||
}
|
||||
}),
|
||||
list: publicProcedure.query(async () => {
|
||||
const docker = getDocker();
|
||||
const containers = await getContainers(docker);
|
||||
return {
|
||||
latest: await getLatest(imageData.name, imageData.tag),
|
||||
};
|
||||
} catch (ex) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: (ex as Error).message,
|
||||
cause: (ex as Error).cause,
|
||||
});
|
||||
}
|
||||
}),
|
||||
list: publicProcedure.query(async () => {
|
||||
const docker = getDocker();
|
||||
const containers = await getContainers(docker);
|
||||
|
||||
// All this data should be local/from the local docker socket/api
|
||||
let dockerInfo = sift(await Promise.all(
|
||||
containers.map(async (container) => {
|
||||
try {
|
||||
if (!container.Id) {
|
||||
throw new Error("No container id could be found");
|
||||
}
|
||||
// All this data should be local/from the local docker socket/api
|
||||
let dockerInfo = sift(
|
||||
await Promise.all(
|
||||
containers.map(async (container) => {
|
||||
try {
|
||||
if (!container.Id) {
|
||||
throw new Error("No container id could be found");
|
||||
}
|
||||
|
||||
const imageData = await getImageData(docker, container);
|
||||
const imageData = await getImageData(docker, container);
|
||||
|
||||
const info = {
|
||||
container: {
|
||||
name: container.Names[0]?.replace("/", "") ?? "",
|
||||
id: container.Id
|
||||
},
|
||||
image: {
|
||||
name: imageData.name,
|
||||
current: {
|
||||
hash: imageData.hash.substring(0, 12),
|
||||
tag: imageData.tag,
|
||||
}
|
||||
}
|
||||
};
|
||||
return info;
|
||||
} catch (ex) {
|
||||
console.error("Error getting container data:", {
|
||||
ex: {
|
||||
message: (ex as Error).message,
|
||||
cause: (ex as Error).cause,
|
||||
},
|
||||
container: {
|
||||
id: container.Id,
|
||||
},
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
));
|
||||
const info = {
|
||||
container: {
|
||||
name: container.Names[0]?.replace("/", "") ?? "",
|
||||
id: container.Id,
|
||||
},
|
||||
image: {
|
||||
name: imageData.name,
|
||||
current: {
|
||||
hash: imageData.hash.substring(0, 12),
|
||||
tag: imageData.tag,
|
||||
},
|
||||
},
|
||||
};
|
||||
return info;
|
||||
} catch (ex) {
|
||||
console.error("Error getting container data:", {
|
||||
ex: {
|
||||
message: (ex as Error).message,
|
||||
cause: (ex as Error).cause,
|
||||
},
|
||||
container: {
|
||||
id: container.Id,
|
||||
},
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (dockerInfo.length === 0) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "No docker containers could be found, check logs for more information",
|
||||
});
|
||||
}
|
||||
if (dockerInfo.length === 0) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: "No docker containers could be found, check logs for more information",
|
||||
});
|
||||
}
|
||||
|
||||
return dockerInfo as unknown as NonNullable<(typeof dockerInfo)[number]>[];
|
||||
}),
|
||||
return dockerInfo as unknown as NonNullable<(typeof dockerInfo)[number]>[];
|
||||
}),
|
||||
});
|
||||
|
||||
async function getImageData(docker: Docker, container: Docker.ContainerInfo) {
|
||||
const inspect = await docker.getImage(container.Image).inspect();
|
||||
const digest = inspect.RepoDigests[0];
|
||||
const containerInspect = await docker.getContainer(container.Id).inspect();
|
||||
const inspect = await docker.getImage(container.Image).inspect();
|
||||
const digest = inspect.RepoDigests[0];
|
||||
const containerInspect = await docker.getContainer(container.Id).inspect();
|
||||
|
||||
let tag = inspect.RepoTags[0]?.split(":")[1];
|
||||
tag ??= containerInspect.Config.Image.split(":")[1];
|
||||
let tag = inspect.RepoTags[0]?.split(":")[1];
|
||||
tag ??= containerInspect.Config.Image.split(":")[1];
|
||||
|
||||
let name = digest?.split("@")?.[0];
|
||||
name ??= container.Image.split(":")[0];
|
||||
let name = digest?.split("@")?.[0];
|
||||
name ??= container.Image.split(":")[0];
|
||||
|
||||
const hash = digest?.split("@")?.[1]?.split(":")?.[1];
|
||||
const hash = digest?.split("@")?.[1]?.split(":")?.[1];
|
||||
|
||||
if (!name) {
|
||||
throw new Error("Container image name could not be found");
|
||||
}
|
||||
if (!hash) {
|
||||
throw new Error("No image hash could be found");
|
||||
}
|
||||
if (!name) {
|
||||
throw new Error("Container image name could not be found");
|
||||
}
|
||||
if (!hash) {
|
||||
throw new Error("No image hash could be found");
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
tag,
|
||||
hash
|
||||
}
|
||||
return {
|
||||
name,
|
||||
tag,
|
||||
hash,
|
||||
};
|
||||
}
|
||||
|
||||
function getDocker() {
|
||||
try {
|
||||
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
|
||||
if (docker) {
|
||||
return docker;
|
||||
}
|
||||
throw new Error("Could not connect to the docker socket");
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Could not connect to docker socket",
|
||||
});
|
||||
}
|
||||
try {
|
||||
const docker = new Docker({ socketPath: "/var/run/docker.sock" });
|
||||
if (docker) {
|
||||
return docker;
|
||||
}
|
||||
throw new Error("Could not connect to the docker socket");
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Could not connect to docker socket",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function getContainers(docker: Docker) {
|
||||
try {
|
||||
const containers = await docker.listContainers();
|
||||
if (containers.length) {
|
||||
return containers;
|
||||
}
|
||||
throw Error("No containers found");
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Could not get containers from docker socket",
|
||||
});
|
||||
}
|
||||
try {
|
||||
const containers = await docker.listContainers();
|
||||
if (containers.length) {
|
||||
return containers;
|
||||
}
|
||||
throw Error("No containers found");
|
||||
} catch (error) {
|
||||
throw new TRPCError({
|
||||
code: "INTERNAL_SERVER_ERROR",
|
||||
message: "Could not get containers from docker socket",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function isSemver(tag: string): boolean {
|
||||
const removeV = tag.replace(/^v/i, "");
|
||||
return !!semver.valid(removeV);
|
||||
const removeV = tag.replace(/^v/i, "");
|
||||
return !!semver.valid(removeV);
|
||||
}
|
||||
|
||||
async function getHash(image: string) {
|
||||
const latestImage = await $`bin/regctl image digest ${image}`.text();
|
||||
const hash = latestImage.split(":")?.[1]?.substring(0, 12);
|
||||
if (!hash) {
|
||||
throw new Error("Hash not found", {
|
||||
cause: {
|
||||
imageDigest: latestImage,
|
||||
},
|
||||
});
|
||||
}
|
||||
return hash;
|
||||
const latestImage = await $`bin/regctl image digest ${image}`.text();
|
||||
const hash = latestImage.split(":")?.[1]?.substring(0, 12);
|
||||
if (!hash) {
|
||||
throw new Error("Hash not found", {
|
||||
cause: {
|
||||
imageDigest: latestImage,
|
||||
},
|
||||
});
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
async function getLatest(image: string, tag?: string): Promise<{ hash: string; tag?: string }> {
|
||||
if (tag && isSemver(tag)) {
|
||||
return await getSemverTag(image);
|
||||
}
|
||||
if (tag && isSemver(tag)) {
|
||||
return await getSemverTag(image);
|
||||
}
|
||||
|
||||
let imageSh = image;
|
||||
if (tag) {
|
||||
imageSh += `:${tag}`;
|
||||
}
|
||||
let imageSh = image;
|
||||
if (tag) {
|
||||
imageSh += `:${tag}`;
|
||||
}
|
||||
|
||||
const hash = await getHash(imageSh);
|
||||
const hash = await getHash(imageSh);
|
||||
|
||||
return {
|
||||
hash,
|
||||
};
|
||||
return {
|
||||
hash,
|
||||
};
|
||||
}
|
||||
|
||||
async function getSemverTag(image: string): Promise<{ hash: string; tag?: string }> {
|
||||
const allTags = await $`bin/regctl tag ls --exclude 'version.*' --exclude 'unstable.*' --exclude '.*rc.*' --exclude 'lib.*' --exclude 'release.*' --exclude 'arm.*
|
||||
const allTags = await $`bin/regctl tag ls --exclude 'version.*' --exclude 'unstable.*' --exclude '.*rc.*' --exclude 'lib.*' --exclude 'release.*' --exclude 'arm.*
|
||||
' --exclude 'amd.*' ${image}`.text();
|
||||
const semverTags = allTags.split("\n").filter((tag) => isSemver(tag));
|
||||
const newestTag = semver.rsort(semverTags)[0];
|
||||
const semverTags = allTags.split("\n").filter((tag) => isSemver(tag));
|
||||
const newestTag = semver.rsort(semverTags)[0];
|
||||
|
||||
let imageSh = image;
|
||||
if (newestTag) {
|
||||
imageSh += `:${newestTag}`;
|
||||
return {
|
||||
hash: await getHash(imageSh),
|
||||
tag: newestTag,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hash: await getHash(imageSh),
|
||||
};
|
||||
let imageSh = image;
|
||||
if (newestTag) {
|
||||
imageSh += `:${newestTag}`;
|
||||
return {
|
||||
hash: await getHash(imageSh),
|
||||
tag: newestTag,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hash: await getHash(imageSh),
|
||||
};
|
||||
}
|
||||
|
||||
export type dockerRouterType = inferRouterOutputs<typeof dockerRouter>;
|
||||
export type dockerRouterType = inferRouterOutputs<typeof dockerRouter>;
|
||||
|
||||
Reference in New Issue
Block a user