mirror of
https://github.com/CyberL1/dlinux-dashboard.git
synced 2025-01-22 01:19:18 -05:00
feat:(api): add a way to reinstall container's image
This commit is contained in:
parent
db464cd92a
commit
6a986d012d
39
src/routes/containers/_id/reinstall.ts
Normal file
39
src/routes/containers/_id/reinstall.ts
Normal 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();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
@ -5,6 +5,7 @@ export interface Container {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface CreateContainerBody {
|
export interface CreateContainerBody {
|
||||||
|
name: string;
|
||||||
image: string;
|
image: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,8 +17,9 @@ export const getContainer = (id: string) => {
|
|||||||
return container;
|
return container;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createContainer = ({ image }: CreateContainerBody) => {
|
export const createContainer = ({ name, image }: CreateContainerBody) => {
|
||||||
const container = dockerode.createContainer({
|
const container = dockerode.createContainer({
|
||||||
|
name,
|
||||||
Image: `code-containers/${image}`,
|
Image: `code-containers/${image}`,
|
||||||
Labels: {
|
Labels: {
|
||||||
"code-containers.image": image,
|
"code-containers.image": image,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user