feat:(api): add a way to reinstall container's image

This commit is contained in:
CyberL1 2025-01-10 13:04:36 -05:00
parent db464cd92a
commit 6a986d012d
3 changed files with 42 additions and 1 deletions

View File

@ -0,0 +1,39 @@
import type {
CreateContainerBody,
RemoveContainerParams,
RemoveContainerQuery,
} from "#src/types/Container.ts";
import { createContainer, getContainer } from "#src/utils/containers.ts";
import { getImage } from "#src/utils/images.ts";
import type { FastifyInstance, FastifyRequest } from "fastify";
export default (fastify: FastifyInstance) => {
fastify.post(
"/",
async (
req: FastifyRequest<{
Params: RemoveContainerParams;
Querystring: RemoveContainerQuery;
Body: CreateContainerBody;
}>,
) => {
if (!(await getImage(req.body?.image).inspect()).Id) {
return;
}
const oldContainer = getContainer(req.params.id);
const name = (await oldContainer.inspect()).Name;
const image =
req.body?.image ||
(await oldContainer.inspect()).Config.Labels["code-containers.image"];
await oldContainer.remove({ force: req.query.force === "true" });
const newContainer = await createContainer({ name, image });
await newContainer.start();
return newContainer.inspect();
},
);
};

View File

@ -5,6 +5,7 @@ export interface Container {
}
export interface CreateContainerBody {
name: string;
image: string;
}

View File

@ -17,8 +17,9 @@ export const getContainer = (id: string) => {
return container;
};
export const createContainer = ({ image }: CreateContainerBody) => {
export const createContainer = ({ name, image }: CreateContainerBody) => {
const container = dockerode.createContainer({
name,
Image: `code-containers/${image}`,
Labels: {
"code-containers.image": image,