diff --git a/src/routes/containers/_id/reinstall.ts b/src/routes/containers/_id/reinstall.ts new file mode 100644 index 0000000..c93beef --- /dev/null +++ b/src/routes/containers/_id/reinstall.ts @@ -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(); + }, + ); +}; diff --git a/src/types/Container.ts b/src/types/Container.ts index 1a2a36c..723c80b 100644 --- a/src/types/Container.ts +++ b/src/types/Container.ts @@ -5,6 +5,7 @@ export interface Container { } export interface CreateContainerBody { + name: string; image: string; } diff --git a/src/utils/containers.ts b/src/utils/containers.ts index f566884..c589b1f 100644 --- a/src/utils/containers.ts +++ b/src/utils/containers.ts @@ -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,