2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-01-04 12:16:09 +00:00
|
|
|
import PortainerError from '@/portainer/error';
|
|
|
|
import axios from '@/portainer/services/axios';
|
2022-06-26 14:16:50 +00:00
|
|
|
import { genericHandler } from '@/docker/rest/response/handlers';
|
2022-01-04 12:16:09 +00:00
|
|
|
|
|
|
|
import { ContainerId, DockerContainer } from './types';
|
|
|
|
|
|
|
|
export async function startContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId
|
|
|
|
) {
|
|
|
|
await axios.post<void>(
|
|
|
|
urlBuilder(endpointId, id, 'start'),
|
|
|
|
{},
|
|
|
|
{ transformResponse: genericHandler }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function stopContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId
|
|
|
|
) {
|
|
|
|
await axios.post<void>(urlBuilder(endpointId, id, 'stop'), {});
|
|
|
|
}
|
|
|
|
|
2023-05-29 21:36:10 +00:00
|
|
|
export async function recreateContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId,
|
|
|
|
pullImage: boolean
|
|
|
|
) {
|
|
|
|
await axios.post<void>(`/docker/${endpointId}/containers/${id}/recreate`, {
|
|
|
|
PullImage: pullImage,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-04 12:16:09 +00:00
|
|
|
export async function restartContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId
|
|
|
|
) {
|
|
|
|
await axios.post<void>(urlBuilder(endpointId, id, 'restart'), {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function killContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId
|
|
|
|
) {
|
|
|
|
await axios.post<void>(urlBuilder(endpointId, id, 'kill'), {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function pauseContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId
|
|
|
|
) {
|
|
|
|
await axios.post<void>(urlBuilder(endpointId, id, 'pause'), {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function resumeContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId
|
|
|
|
) {
|
|
|
|
await axios.post<void>(urlBuilder(endpointId, id, 'unpause'), {});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function renameContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
id: ContainerId,
|
|
|
|
name: string
|
|
|
|
) {
|
|
|
|
await axios.post<void>(
|
|
|
|
urlBuilder(endpointId, id, 'rename'),
|
|
|
|
{},
|
|
|
|
{ params: { name }, transformResponse: genericHandler }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function removeContainer(
|
|
|
|
endpointId: EnvironmentId,
|
|
|
|
container: DockerContainer,
|
|
|
|
removeVolumes: boolean
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const { data } = await axios.delete<null | { message: string }>(
|
|
|
|
urlBuilder(endpointId, container.Id),
|
|
|
|
{
|
|
|
|
params: { v: removeVolumes ? 1 : 0, force: true },
|
|
|
|
transformResponse: genericHandler,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (data && data.message) {
|
|
|
|
throw new PortainerError(data.message);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
throw new PortainerError('Unable to remove container', e as Error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 04:33:29 +00:00
|
|
|
export function urlBuilder(
|
2022-01-04 12:16:09 +00:00
|
|
|
endpointId: EnvironmentId,
|
2022-05-09 21:01:15 +00:00
|
|
|
id?: ContainerId,
|
2022-01-04 12:16:09 +00:00
|
|
|
action?: string
|
|
|
|
) {
|
2022-05-09 21:01:15 +00:00
|
|
|
let url = `/endpoints/${endpointId}/docker/containers`;
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
url += `/${id}`;
|
|
|
|
}
|
2022-01-04 12:16:09 +00:00
|
|
|
|
|
|
|
if (action) {
|
2022-05-09 21:01:15 +00:00
|
|
|
url += `/${action}`;
|
2022-01-04 12:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|