1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

feat(app): limit the docker API version supported by the frontend (#12295)
Some checks failed
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
ci / build_images (map[arch:arm platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Has been cancelled
/ triage (push) Has been cancelled
Lint / Run linters (push) Has been cancelled
Test / test-client (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:linux]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
Test / test-server (map[arch:arm64 platform:linux]) (push) Has been cancelled
ci / build_manifests (push) Has been cancelled

This commit is contained in:
LP B 2024-10-08 17:13:14 +02:00 committed by GitHub
parent 8cbd23c059
commit ac5491e864
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
227 changed files with 4702 additions and 3411 deletions

View file

@ -3,15 +3,17 @@ import { Heart, Power } from 'lucide-react';
import { Icon } from '@/react/components/Icon';
import {
DockerContainer,
ContainerListViewModel,
ContainerStatus as Status,
} from '../containers/types';
interface Props {
containers: DockerContainer[];
containers: ContainerListViewModel[];
}
export function useContainerStatusComponent(containers: DockerContainer[]) {
export function useContainerStatusComponent(
containers: ContainerListViewModel[]
) {
return <ContainerStatus containers={containers} />;
}
@ -42,23 +44,23 @@ export function ContainerStatus({ containers }: Props) {
);
}
function runningContainersFilter(containers: DockerContainer[]) {
function runningContainersFilter(containers: ContainerListViewModel[]) {
return containers.filter(
(container) =>
container.Status === Status.Running || container.Status === Status.Healthy
).length;
}
function stoppedContainersFilter(containers: DockerContainer[]) {
function stoppedContainersFilter(containers: ContainerListViewModel[]) {
return containers.filter(
(container) =>
container.Status === Status.Exited || container.Status === Status.Stopped
).length;
}
function healthyContainersFilter(containers: DockerContainer[]) {
function healthyContainersFilter(containers: ContainerListViewModel[]) {
return containers.filter((container) => container.Status === Status.Healthy)
.length;
}
function unhealthyContainersFilter(containers: DockerContainer[]) {
function unhealthyContainersFilter(containers: ContainerListViewModel[]) {
return containers.filter((container) => container.Status === Status.Unhealthy)
.length;
}

View file

@ -0,0 +1,41 @@
import { useQuery } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { queryKeys } from '../queries/utils';
import { buildDockerUrl } from '../queries/utils/buildDockerUrl';
interface DashboardResponse {
containers: {
total: number;
running: number;
stopped: number;
healthy: number;
unhealthy: number;
};
services: number;
images: {
total: number;
size: number;
};
volumes: number;
networks: number;
stacks: number;
}
export function useDashboard(envId: EnvironmentId) {
return useQuery({
queryFn: async () => {
try {
const res = await axios.get<DashboardResponse>(
buildDockerUrl(envId, 'dashboard')
);
return res.data;
} catch (error) {
throw parseAxiosError(error);
}
},
queryKey: [...queryKeys.root(envId), 'dashboard'] as const,
});
}

View file

@ -6,7 +6,7 @@ import axios, {
} from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl } from '../../proxy/queries/build-url';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
export function useApiVersion(environmentId: EnvironmentId) {
return useQuery(['environment', environmentId, 'agent', 'ping'], () =>
@ -16,7 +16,9 @@ export function useApiVersion(environmentId: EnvironmentId) {
async function getApiVersion(environmentId: EnvironmentId) {
try {
const { headers } = await axios.get(buildUrl(environmentId, 'ping'));
const { headers } = await axios.get(
buildDockerProxyUrl(environmentId, 'ping')
);
return parseInt(headers['portainer-agent-api-version'], 10) || 1;
} catch (error) {
// 404 - agent is up - set version to 1

View file

@ -1,7 +1,7 @@
import { createColumnHelper } from '@tanstack/react-table';
import { isoDate } from '@/portainer/filters/filters';
import { createOwnershipColumn } from '@/react/docker/components/datatable/createOwnershipColumn';
import { createOwnershipColumn } from '@/react/docker/components/datatables/createOwnershipColumn';
import { buildNameColumn } from '@@/datatables/buildNameColumn';

View file

@ -0,0 +1,21 @@
import { Config } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { DockerConfig } from '../types';
export async function getConfig(
environmentId: EnvironmentId,
configId: DockerConfig['Id']
) {
try {
const { data } = await axios.get<Config>(
buildDockerProxyUrl(environmentId, 'configs', configId)
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to retrieve config');
}
}

View file

@ -0,0 +1,17 @@
import { Config } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
export async function getConfigs(environmentId: EnvironmentId) {
try {
const { data } = await axios.get<Config[]>(
buildDockerProxyUrl(environmentId, 'configs')
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to retrieve configs');
}
}

View file

@ -0,0 +1,22 @@
import { ConfigSpec } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { PortainerResponse } from '../../types';
export async function createConfig(
environmentId: EnvironmentId,
config: ConfigSpec
) {
try {
const { data } = await axios.post<PortainerResponse<{ Id: string }>>(
buildDockerProxyUrl(environmentId, 'configs', 'create'),
config
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to create config');
}
}

View file

@ -0,0 +1,16 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { DockerConfig } from '../types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
export async function deleteConfig(
environmentId: EnvironmentId,
id: DockerConfig['Id']
) {
try {
await axios.delete(buildDockerProxyUrl(environmentId, 'configs', id));
} catch (e) {
throw parseAxiosError(e, 'Unable to delete config');
}
}

View file

@ -3,13 +3,13 @@ import { ResourceControlOwnership } from '@/react/portainer/access-control/types
import { UserId } from '@/portainer/users/types';
import { getDefaultImageConfig } from '@/react/portainer/registries/utils/getImageConfig';
import { ContainerResponse } from '../../queries/container';
import { ContainerDetailsResponse } from '../../queries/useContainer';
import { toViewModel as toPortsMappingViewModel } from './PortsMappingField.viewModel';
import { Values } from './BaseForm';
export function toViewModel(
config: ContainerResponse,
config: ContainerDetailsResponse,
isPureAdmin: boolean,
currentUserId: UserId,
nodeName: string,

View file

@ -1,9 +1,9 @@
import { ContainerJSON } from '@/react/docker/containers/queries/container';
import { ContainerDetailsJSON } from '@/react/docker/containers/queries/useContainer';
import { capabilities } from './types';
import { Values } from './CapabilitiesTab';
export function toViewModel(config: ContainerJSON): Values {
export function toViewModel(config: ContainerDetailsJSON): Values {
const { CapAdd, CapDrop } = getDefaults(config);
const missingCaps = capabilities
@ -15,7 +15,7 @@ export function toViewModel(config: ContainerJSON): Values {
return [...CapAdd, ...missingCaps];
function getDefaults(config: ContainerJSON) {
function getDefaults(config: ContainerDetailsJSON) {
return {
CapAdd: config.HostConfig?.CapAdd || [],
CapDrop: config.HostConfig?.CapDrop || [],

View file

@ -2,7 +2,7 @@ import { HostConfig } from 'docker-types/generated/1.41';
import { commandArrayToString } from '@/docker/helpers/containers';
import { ContainerJSON } from '../../queries/container';
import { ContainerDetailsJSON } from '../../queries/useContainer';
import { ConsoleConfig, ConsoleSetting } from './ConsoleSettings';
import { LogConfig } from './LoggerConfig';
@ -19,7 +19,7 @@ export function getDefaultViewModel(): Values {
};
}
export function toViewModel(config: ContainerJSON): Values {
export function toViewModel(config: ContainerDetailsJSON): Values {
if (!config.Config) {
return getDefaultViewModel();
}

View file

@ -19,7 +19,7 @@ import { InformationPanel } from '@@/InformationPanel';
import { TextTip } from '@@/Tip/TextTip';
import { HelpLink } from '@@/HelpLink';
import { useContainers } from '../queries/containers';
import { useContainers } from '../queries/useContainers';
import { useSystemLimits, useIsWindows } from '../../proxy/queries/useInfo';
import { useCreateOrReplaceMutation } from './useCreateMutation';
@ -149,7 +149,21 @@ function CreateForm() {
const config = toRequest(values, registry, hideCapabilities);
return mutation.mutate(
{ config, environment, values, registry, oldContainer, extraNetworks },
{
config,
environment,
values: {
accessControl: values.accessControl,
imageName: values.image.image,
name: values.name,
alwaysPull: values.alwaysPull,
enableWebhook: values.enableWebhook,
nodeName: values.nodeName,
},
registry,
oldContainer,
extraNetworks,
},
{
onSuccess() {
sendAnalytics(values, registry);

View file

@ -1,11 +1,11 @@
import { parseArrayOfStrings } from '@@/form-components/EnvironmentVariablesFieldset/utils';
import { ContainerJSON } from '../../queries/container';
import { ContainerDetailsJSON } from '../../queries/useContainer';
export function getDefaultViewModel() {
return [];
}
export function toViewModel(container: ContainerJSON) {
export function toViewModel(container: ContainerDetailsJSON) {
return parseArrayOfStrings(container.Config?.Env);
}

View file

@ -1,8 +1,8 @@
import { ContainerJSON } from '../../queries/container';
import { ContainerDetailsJSON } from '../../queries/useContainer';
import { Values } from './types';
export function toViewModel(config: ContainerJSON): Values {
export function toViewModel(config: ContainerDetailsJSON): Values {
if (!config || !config.Config || !config.Config.Labels) {
return [];
}

View file

@ -2,7 +2,7 @@ import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
import { Option, PortainerSelect } from '@@/form-components/PortainerSelect';
import { useContainers } from '../../queries/containers';
import { useContainers } from '../../queries/useContainers';
import { ContainerStatus } from '../../types';
export function ContainerSelector({

View file

@ -1,7 +1,7 @@
import { DockerNetwork } from '@/react/docker/networks/types';
import { ContainerJSON } from '../../queries/container';
import { DockerContainer } from '../../types';
import { ContainerDetailsJSON } from '../../queries/useContainer';
import { ContainerListViewModel } from '../../types';
import { CONTAINER_MODE, Values } from './types';
@ -22,9 +22,9 @@ export function getDefaultViewModel(isWindows: boolean) {
}
export function toViewModel(
config: ContainerJSON,
config: ContainerDetailsJSON,
networks: Array<DockerNetwork>,
runningContainers: Array<DockerContainer> = []
runningContainers: Array<ContainerListViewModel> = []
): Values {
const dns = config.HostConfig?.Dns;
const [primaryDns = '', secondaryDns = ''] = dns || [];
@ -62,9 +62,9 @@ export function toViewModel(
}
function getNetworkMode(
config: ContainerJSON,
config: ContainerDetailsJSON,
networks: Array<DockerNetwork>,
runningContainers: Array<DockerContainer> = []
runningContainers: Array<ContainerListViewModel> = []
) {
let networkMode = config.HostConfig?.NetworkMode || '';
if (!networkMode) {

View file

@ -4,7 +4,7 @@ import { useCurrentStateAndParams } from '@uirouter/react';
import { useState } from 'react';
import { FormikHelpers } from 'formik/dist/types';
import { invalidateContainer } from '@/react/docker/containers/queries/container';
import { invalidateContainer } from '@/react/docker/containers/queries/useContainer';
import { notifySuccess } from '@/portainer/services/notifications';
import { mutationOptions, withError } from '@/react-tools/react-query';
import { useSystemLimits } from '@/react/docker/proxy/queries/useInfo';

View file

@ -1,11 +1,11 @@
import { ContainerJSON } from '../../queries/container';
import { ContainerDetailsJSON } from '../../queries/useContainer';
import { toDevicesViewModel } from './DevicesField';
import { gpuFieldsetUtils } from './GpuFieldset';
import { toViewModelCpu, toViewModelMemory } from './memory-utils';
import { Values } from './ResourcesTab';
export function toViewModel(config: ContainerJSON): Values {
export function toViewModel(config: ContainerDetailsJSON): Values {
return {
runtime: {
privileged: config.HostConfig?.Privileged || false,

View file

@ -1,8 +1,8 @@
import { ContainerJSON } from '../../queries/container';
import { ContainerDetailsJSON } from '../../queries/useContainer';
import { RestartPolicy } from './types';
export function toViewModel(config: ContainerJSON): RestartPolicy {
export function toViewModel(config: ContainerDetailsJSON): RestartPolicy {
switch (config.HostConfig?.RestartPolicy?.Name) {
case 'always':
return RestartPolicy.Always;

View file

@ -1,8 +1,8 @@
import { ContainerJSON } from '../../queries/container';
import { ContainerDetailsJSON } from '../../queries/useContainer';
import { VolumeType, Values } from './types';
export function toViewModel(config: ContainerJSON): Values {
export function toViewModel(config: ContainerDetailsJSON): Values {
return Object.values(config.Mounts || {}).map((mount) => ({
type: (mount.Type || 'volume') as VolumeType,
name: mount.Name || mount.Source || '',

View file

@ -1,5 +1,4 @@
import { useMutation, useQueryClient } from 'react-query';
import { RawAxiosRequestHeaders } from 'axios';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import {
@ -32,12 +31,13 @@ import {
renameContainer,
startContainer,
stopContainer,
urlBuilder,
} from '../containers.service';
import { PortainerResponse } from '../../types';
import { connectContainer } from '../../networks/queries/useConnectContainer';
import { DockerContainer } from '../types';
import { connectContainer } from '../../networks/queries/useConnectContainerMutation';
import { ContainerListViewModel } from '../types';
import { queryKeys } from '../queries/query-keys';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { CreateContainerRequest } from './types';
import { Values } from './useInitialValues';
@ -62,13 +62,20 @@ export function useCreateOrReplaceMutation() {
interface CreateOptions {
config: CreateContainerRequest;
values: Values;
values: {
name: Values['name'];
imageName: string;
accessControl: Values['accessControl'];
nodeName?: Values['nodeName'];
alwaysPull?: Values['alwaysPull'];
enableWebhook?: Values['enableWebhook'];
};
registry?: Registry;
environment: Environment;
}
interface ReplaceOptions extends CreateOptions {
oldContainer: DockerContainer;
oldContainer: ContainerListViewModel;
extraNetworks: Array<ExtraNetwork>;
}
@ -90,14 +97,14 @@ async function create({
}: CreateOptions) {
await pullImageIfNeeded(
environment.Id,
values.alwaysPull || false,
values.imageName,
values.nodeName,
values.alwaysPull,
values.image.image,
registry
);
const containerResponse = await createAndStart(
environment,
environment.Id,
config,
values.name,
values.nodeName
@ -106,8 +113,8 @@ async function create({
await applyContainerSettings(
containerResponse.Id,
environment,
values.enableWebhook,
values.accessControl,
values.enableWebhook,
containerResponse.Portainer?.ResourceControl,
registry
);
@ -123,37 +130,38 @@ async function replace({
}: ReplaceOptions) {
await pullImageIfNeeded(
environment.Id,
values.alwaysPull || false,
values.imageName,
values.nodeName,
values.alwaysPull,
values.image.image,
registry
);
const containerResponse = await renameAndCreate(
environment,
values,
environment.Id,
values.name,
oldContainer,
config
config,
values.nodeName
);
await applyContainerSettings(
containerResponse.Id,
environment,
values.enableWebhook,
values.accessControl,
values.enableWebhook,
containerResponse.Portainer?.ResourceControl,
registry
);
await connectToExtraNetworks(
environment.Id,
values.nodeName,
containerResponse.Id,
extraNetworks
extraNetworks,
values.nodeName
);
await removeContainer(environment.Id, oldContainer.Id, {
nodeName: values.nodeName,
nodeName: oldContainer.NodeName,
});
}
@ -162,33 +170,33 @@ async function replace({
* on any failure, it will rename the old container to its original name
*/
async function renameAndCreate(
environment: Environment,
values: Values,
oldContainer: DockerContainer,
config: CreateContainerRequest
environmentId: EnvironmentId,
name: string,
oldContainer: ContainerListViewModel,
config: CreateContainerRequest,
nodeName?: string
) {
let renamed = false;
try {
await stopContainerIfNeeded(environment.Id, values.nodeName, oldContainer);
await stopContainerIfNeeded(
environmentId,
oldContainer,
oldContainer.NodeName
);
await renameContainer(
environment.Id,
environmentId,
oldContainer.Id,
`${oldContainer.Names[0]}-old`,
{ nodeName: values.nodeName }
{ nodeName: oldContainer.NodeName }
);
renamed = true;
return await createAndStart(
environment,
config,
values.name,
values.nodeName
);
return await createAndStart(environmentId, config, name, nodeName);
} catch (e) {
if (renamed) {
await renameContainer(environment.Id, oldContainer.Id, values.name, {
nodeName: values.nodeName,
await renameContainer(environmentId, oldContainer.Id, name, {
nodeName: oldContainer.NodeName,
});
}
throw e;
@ -201,8 +209,8 @@ async function renameAndCreate(
async function applyContainerSettings(
containerId: string,
environment: Environment,
enableWebhook: boolean,
accessControl: AccessControlFormData,
enableWebhook?: boolean,
resourceControl?: ResourceControlResponse,
registry?: Registry
) {
@ -224,15 +232,15 @@ async function applyContainerSettings(
* on failure, it will remove the new container
*/
async function createAndStart(
environment: Environment,
environmentId: EnvironmentId,
config: CreateContainerRequest,
name: string,
nodeName: string
name?: string,
nodeName?: string
) {
let containerId = '';
try {
const containerResponse = await createContainer(
environment.Id,
environmentId,
config,
name,
{
@ -242,11 +250,11 @@ async function createAndStart(
containerId = containerResponse.Id;
await startContainer(environment.Id, containerResponse.Id, { nodeName });
await startContainer(environmentId, containerResponse.Id, { nodeName });
return containerResponse;
} catch (e) {
if (containerId) {
await removeContainer(environment.Id, containerId, {
await removeContainer(environmentId, containerId, {
nodeName,
});
}
@ -257,9 +265,9 @@ async function createAndStart(
async function pullImageIfNeeded(
environmentId: EnvironmentId,
nodeName: string,
pull: boolean,
image: string,
nodeName?: string,
registry?: Registry
) {
if (!pull) {
@ -282,16 +290,10 @@ async function createContainer(
{ nodeName }: { nodeName?: string } = {}
) {
try {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
const { data } = await axios.post<
PortainerResponse<{ Id: string; Warnings: Array<string> }>
>(urlBuilder(environmentId, undefined, 'create'), config, {
headers,
>(buildDockerProxyUrl(environmentId, 'containers', 'create'), config, {
headers: { ...withAgentTargetHeader(nodeName) },
params: { name },
});
@ -322,9 +324,9 @@ async function createContainerWebhook(
function connectToExtraNetworks(
environmentId: EnvironmentId,
nodeName: string,
containerId: string,
extraNetworks: Array<ExtraNetwork>
extraNetworks: Array<ExtraNetwork>,
nodeName?: string
) {
if (!extraNetworks) {
return null;
@ -345,8 +347,8 @@ function connectToExtraNetworks(
function stopContainerIfNeeded(
environmentId: EnvironmentId,
nodeName: string,
container: DockerContainer
container: ContainerListViewModel,
nodeName?: string
) {
if (container.State !== 'running' || !container.Id) {
return null;

View file

@ -43,8 +43,8 @@ import { useEnvironmentRegistries } from '@/react/portainer/environments/queries
import { EnvVarValues } from '@@/form-components/EnvironmentVariablesFieldset';
import { useNetworksForSelector } from '../components/NetworkSelector';
import { useContainers } from '../queries/containers';
import { useContainer } from '../queries/container';
import { useContainers } from '../queries/useContainers';
import { useContainer } from '../queries/useContainer';
export interface Values extends BaseFormValues {
commands: CommandsTabValues;

View file

@ -3,7 +3,7 @@ import { SchemaOf, object, string } from 'yup';
import { useRouter } from '@uirouter/react';
import { useAuthorizations } from '@/react/hooks/useUser';
import { useConnectContainerMutation } from '@/react/docker/networks/queries/useConnectContainer';
import { useConnectContainerMutation } from '@/react/docker/networks/queries/useConnectContainerMutation';
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
import { FormControl } from '@@/form-components/FormControl';

View file

@ -7,7 +7,7 @@ import { useTableState } from '@@/datatables/useTableState';
import { ExpandableDatatable } from '@@/datatables/ExpandableDatatable';
import { withMeta } from '@@/datatables/extend-options/withMeta';
import { DockerContainer } from '../../types';
import { ContainerListViewModel } from '../../types';
import { TableNetwork } from './types';
import { buildColumns } from './columns';
@ -22,7 +22,7 @@ export function ContainerNetworksDatatable({
nodeName,
}: {
dataset: NetworkSettings['Networks'];
container: DockerContainer;
container: ContainerListViewModel;
nodeName?: string;
}) {
const tableState = useTableState(store, storageKey);

View file

@ -2,8 +2,9 @@ import { CellContext } from '@tanstack/react-table';
import { useRouter } from '@uirouter/react';
import { Authorized } from '@/react/hooks/useUser';
import { useDisconnectContainer } from '@/react/docker/networks/queries';
import { useDisconnectContainer } from '@/react/docker/networks/queries/useDisconnectContainerMutation';
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
import { notifySuccess } from '@/portainer/services/notifications';
import { LoadingButton } from '@@/buttons';
@ -17,19 +18,25 @@ export function buildActions({ nodeName }: { nodeName?: string } = {}) {
});
function Cell({
row,
row: {
original: { id: networkId },
},
table: {
options: { meta },
},
}: CellContext<TableNetwork, unknown>) {
const router = useRouter();
const environmentId = useEnvironmentId();
const disconnectMutation = useDisconnectContainer();
const disconnectMutation = useDisconnectContainer({
environmentId,
networkId,
});
return (
<Authorized authorizations="DockerNetworkDisconnect">
<LoadingButton
color="dangerlight"
data-cy="disconnect-network-button"
isLoading={disconnectMutation.isLoading}
loadingText="Leaving network..."
type="button"
@ -47,13 +54,12 @@ export function buildActions({ nodeName }: { nodeName?: string } = {}) {
disconnectMutation.mutate(
{
environmentId,
networkId: row.original.id,
containerId: meta.containerId,
nodeName,
},
{
onSuccess() {
notifySuccess('Container successfully disconnected', networkId);
router.stateService.reload();
},
}

View file

@ -1,7 +1,7 @@
import { Box } from 'lucide-react';
import { ContainerListViewModel } from '@/react/docker/containers/types';
import { Environment } from '@/react/portainer/environments/types';
import type { DockerContainer } from '@/react/docker/containers/types';
import { useShowGPUsColumn } from '@/react/docker/containers/utils';
import { Table, Datatable } from '@@/datatables';
@ -16,7 +16,7 @@ import {
import { TableSettingsProvider } from '@@/datatables/useTableSettings';
import { useTableState } from '@@/datatables/useTableState';
import { useContainers } from '../../queries/containers';
import { useContainers } from '../../queries/useContainers';
import { createStore } from './datatable-store';
import { ContainersDatatableSettings } from './ContainersDatatableSettings';
@ -72,7 +72,7 @@ export function ContainersDatatable({
initialTableState={getColumnVisibilityState(tableState.hiddenColumns)}
renderTableSettings={(tableInstance) => (
<>
<ColumnVisibilityMenu<DockerContainer>
<ColumnVisibilityMenu<ContainerListViewModel>
table={tableInstance}
onChange={(hiddenColumns) => {
tableState.setHiddenColumns(hiddenColumns);

View file

@ -16,7 +16,7 @@ import { setPortainerAgentTargetHeader } from '@/portainer/services/http-request
import {
ContainerId,
ContainerStatus,
DockerContainer,
ContainerListViewModel,
} from '@/react/docker/containers/types';
import {
killContainer,
@ -38,7 +38,7 @@ type ContainerServiceAction = (
) => Promise<void>;
interface Props {
selectedItems: DockerContainer[];
selectedItems: ContainerListViewModel[];
isAddActionVisible: boolean;
endpointId: EnvironmentId;
}
@ -175,7 +175,7 @@ export function ContainersDatatableActions({
</div>
);
function onStartClick(selectedItems: DockerContainer[]) {
function onStartClick(selectedItems: ContainerListViewModel[]) {
const successMessage = 'Container successfully started';
const errorMessage = 'Unable to start container';
executeActionOnContainerList(
@ -186,7 +186,7 @@ export function ContainersDatatableActions({
);
}
function onStopClick(selectedItems: DockerContainer[]) {
function onStopClick(selectedItems: ContainerListViewModel[]) {
const successMessage = 'Container successfully stopped';
const errorMessage = 'Unable to stop container';
executeActionOnContainerList(
@ -197,7 +197,7 @@ export function ContainersDatatableActions({
);
}
function onRestartClick(selectedItems: DockerContainer[]) {
function onRestartClick(selectedItems: ContainerListViewModel[]) {
const successMessage = 'Container successfully restarted';
const errorMessage = 'Unable to restart container';
executeActionOnContainerList(
@ -208,7 +208,7 @@ export function ContainersDatatableActions({
);
}
function onKillClick(selectedItems: DockerContainer[]) {
function onKillClick(selectedItems: ContainerListViewModel[]) {
const successMessage = 'Container successfully killed';
const errorMessage = 'Unable to kill container';
executeActionOnContainerList(
@ -219,7 +219,7 @@ export function ContainersDatatableActions({
);
}
function onPauseClick(selectedItems: DockerContainer[]) {
function onPauseClick(selectedItems: ContainerListViewModel[]) {
const successMessage = 'Container successfully paused';
const errorMessage = 'Unable to pause container';
executeActionOnContainerList(
@ -230,7 +230,7 @@ export function ContainersDatatableActions({
);
}
function onResumeClick(selectedItems: DockerContainer[]) {
function onResumeClick(selectedItems: ContainerListViewModel[]) {
const successMessage = 'Container successfully resumed';
const errorMessage = 'Unable to resume container';
executeActionOnContainerList(
@ -241,7 +241,7 @@ export function ContainersDatatableActions({
);
}
async function onRemoveClick(selectedItems: DockerContainer[]) {
async function onRemoveClick(selectedItems: ContainerListViewModel[]) {
const isOneContainerRunning = selectedItems.some(
(container) => container.State === 'running'
);
@ -259,7 +259,7 @@ export function ContainersDatatableActions({
}
async function executeActionOnContainerList(
containers: DockerContainer[],
containers: ContainerListViewModel[],
action: ContainerServiceAction,
successMessage: string,
errorMessage: string
@ -283,7 +283,7 @@ export function ContainersDatatableActions({
}
async function removeSelectedContainers(
containers: DockerContainer[],
containers: ContainerListViewModel[],
removeVolumes: boolean
) {
for (let i = 0; i < containers.length; i += 1) {

View file

@ -1,6 +1,6 @@
import { CellContext } from '@tanstack/react-table';
import type { DockerContainer } from '@/react/docker/containers/types';
import type { ContainerListViewModel } from '@/react/docker/containers/types';
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
import { useContainerGpus } from '@/react/docker/containers/queries/gpus';
@ -14,7 +14,7 @@ export const gpus = columnHelper.display({
function GpusCell({
row: { original: container },
}: CellContext<DockerContainer, unknown>) {
}: CellContext<ContainerListViewModel, unknown>) {
const containerId = container.Id;
const environmentId = useEnvironmentId();
const gpusQuery = useContainerGpus(environmentId, containerId);

View file

@ -1,5 +1,5 @@
import { createColumnHelper } from '@tanstack/react-table';
import { DockerContainer } from '../../../types';
import { ContainerListViewModel } from '../../../types';
export const columnHelper = createColumnHelper<DockerContainer>();
export const columnHelper = createColumnHelper<ContainerListViewModel>();

View file

@ -1,7 +1,7 @@
import { CellContext } from '@tanstack/react-table';
import { useSref } from '@uirouter/react';
import type { DockerContainer } from '@/react/docker/containers/types';
import type { ContainerListViewModel } from '@/react/docker/containers/types';
import { columnHelper } from './helper';
@ -11,7 +11,7 @@ export const image = columnHelper.accessor('Image', {
cell: ImageCell,
});
function ImageCell({ getValue }: CellContext<DockerContainer, string>) {
function ImageCell({ getValue }: CellContext<ContainerListViewModel, string>) {
const imageName = getValue();
const linkProps = useSref('docker.images.image', { id: imageName });
const shortImageName = trimSHASum(imageName);

View file

@ -1,8 +1,8 @@
import _ from 'lodash';
import { useMemo } from 'react';
import { createOwnershipColumn } from '@/react/docker/components/datatable/createOwnershipColumn';
import { DockerContainer } from '@/react/docker/containers/types';
import { createOwnershipColumn } from '@/react/docker/components/datatables/createOwnershipColumn';
import { ContainerListViewModel } from '@/react/docker/containers/types';
import { created } from './created';
import { host } from './host';
@ -32,7 +32,7 @@ export function useColumns(
isHostColumnVisible && host,
isGPUsColumnVisible && gpus,
ports,
createOwnershipColumn<DockerContainer>(),
createOwnershipColumn<ContainerListViewModel>(),
]),
[isHostColumnVisible, isGPUsColumnVisible]
);

View file

@ -2,7 +2,7 @@ import { CellContext } from '@tanstack/react-table';
import _ from 'lodash';
import { useSref } from '@uirouter/react';
import type { DockerContainer } from '@/react/docker/containers/types';
import type { ContainerListViewModel } from '@/react/docker/containers/types';
import { useTableSettings } from '@@/datatables/useTableSettings';
@ -19,7 +19,7 @@ export const name = columnHelper.accessor((row) => row.Names[0], {
export function NameCell({
getValue,
row: { original: container },
}: CellContext<DockerContainer, string>) {
}: CellContext<ContainerListViewModel, string>) {
const name = getValue();
const linkProps = useSref('.container', {

View file

@ -1,8 +1,8 @@
import _ from 'lodash';
import { CellContext } from '@tanstack/react-table';
import type { DockerContainer } from '@/react/docker/containers/types';
import { PublishedPortLink } from '@/react/docker/components/ImageStatus/PublishedPortLink';
import type { ContainerListViewModel } from '@/react/docker/containers/types';
import { useRowContext } from '../RowContext';
@ -20,7 +20,7 @@ export const ports = columnHelper.accessor(
}
);
function Cell({ row }: CellContext<DockerContainer, string>) {
function Cell({ row }: CellContext<ContainerListViewModel, string>) {
const ports = row.original.Ports;
const { environment } = useRowContext();

View file

@ -2,7 +2,7 @@ import { CellContext } from '@tanstack/react-table';
import { useAuthorizations } from '@/react/hooks/useUser';
import { ContainerQuickActions } from '@/react/docker/containers/components/ContainerQuickActions';
import { DockerContainer } from '@/react/docker/containers/types';
import { ContainerListViewModel } from '@/react/docker/containers/types';
import { useTableSettings } from '@@/datatables/useTableSettings';
@ -18,7 +18,7 @@ export const quickActions = columnHelper.display({
function QuickActionsCell({
row: { original: container },
}: CellContext<DockerContainer, unknown>) {
}: CellContext<ContainerListViewModel, unknown>) {
const settings = useTableSettings<TableSettings>();
const { hiddenQuickActions = [] } = settings;

View file

@ -2,7 +2,7 @@ import clsx from 'clsx';
import { CellContext } from '@tanstack/react-table';
import {
type DockerContainer,
type ContainerListViewModel,
ContainerStatus,
} from '@/react/docker/containers/types';
@ -25,7 +25,7 @@ export const state = columnHelper.accessor('Status', {
function StatusCell({
getValue,
row: { original: container },
}: CellContext<DockerContainer, ContainerStatus>) {
}: CellContext<ContainerListViewModel, ContainerStatus>) {
const status = getValue();
const hasHealthCheck = [

View file

@ -1,6 +1,6 @@
import { useCurrentStateAndParams } from '@uirouter/react';
import { useContainer } from '@/react/docker/containers/queries/container';
import { useContainer } from '@/react/docker/containers/queries/useContainer';
import { InformationPanel } from '@@/InformationPanel';
import { TextTip } from '@@/Tip/TextTip';

View file

@ -1,28 +1,27 @@
import { RawAxiosRequestHeaders } from 'axios';
import _ from 'lodash';
import { EnvironmentId } from '@/react/portainer/environments/types';
import PortainerError from '@/portainer/error';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { genericHandler } from '@/docker/rest/response/handlers';
import { ContainerId } from './types';
import { withAgentTargetHeader } from '../proxy/queries/utils';
import { buildDockerProxyUrl } from '../proxy/queries/buildDockerProxyUrl';
import { buildDockerUrl } from '../queries/utils/buildDockerUrl';
import { ContainerId, ContainerLogsParams } from './types';
export async function startContainer(
environmentId: EnvironmentId,
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
try {
await axios.post<void>(
urlBuilder(environmentId, id, 'start'),
buildDockerProxyUrl(environmentId, 'containers', id, 'start'),
{},
{ transformResponse: genericHandler, headers }
{
headers: { ...withAgentTargetHeader(nodeName) },
}
);
} catch (e) {
throw parseAxiosError(e, 'Failed starting container');
@ -34,13 +33,15 @@ export async function stopContainer(
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
try {
await axios.post<void>(
buildDockerProxyUrl(endpointId, 'containers', id, 'stop'),
{},
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (e) {
throw parseAxiosError(e, 'Failed stopping container');
}
await axios.post<void>(urlBuilder(endpointId, id, 'stop'), {}, { headers });
}
export async function recreateContainer(
@ -49,19 +50,17 @@ export async function recreateContainer(
pullImage: boolean,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
try {
await axios.post<void>(
buildDockerUrl(endpointId, 'containers', id, 'recreate'),
{
PullImage: pullImage,
},
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (e) {
throw parseAxiosError(e, 'Failed recreating container');
}
await axios.post<void>(
`/docker/${endpointId}/containers/${id}/recreate`,
{
PullImage: pullImage,
},
{ headers }
);
}
export async function restartContainer(
@ -69,17 +68,15 @@ export async function restartContainer(
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
try {
await axios.post<void>(
buildDockerProxyUrl(endpointId, 'containers', id, 'restart'),
{},
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (e) {
throw parseAxiosError(e, 'Failed restarting container');
}
await axios.post<void>(
urlBuilder(endpointId, id, 'restart'),
{},
{ headers }
);
}
export async function killContainer(
@ -87,13 +84,15 @@ export async function killContainer(
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
try {
await axios.post<void>(
buildDockerProxyUrl(endpointId, 'containers', id, 'kill'),
{},
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (e) {
throw parseAxiosError(e, 'Failed killing container');
}
await axios.post<void>(urlBuilder(endpointId, id, 'kill'), {}, { headers });
}
export async function pauseContainer(
@ -101,13 +100,15 @@ export async function pauseContainer(
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
try {
await axios.post<void>(
buildDockerProxyUrl(endpointId, 'containers', id, 'pause'),
{},
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (e) {
throw parseAxiosError(e, 'Failed pausing container');
}
await axios.post<void>(urlBuilder(endpointId, id, 'pause'), {}, { headers });
}
export async function resumeContainer(
@ -115,17 +116,15 @@ export async function resumeContainer(
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
try {
await axios.post<void>(
buildDockerProxyUrl(endpointId, 'containers', id, 'unpause'),
{},
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (e) {
throw parseAxiosError(e, 'Failed resuming container');
}
await axios.post<void>(
urlBuilder(endpointId, id, 'unpause'),
{},
{ headers }
);
}
export async function renameContainer(
@ -134,17 +133,18 @@ export async function renameContainer(
name: string,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
try {
await axios.post<void>(
buildDockerProxyUrl(endpointId, 'containers', id, 'rename'),
{},
{
params: { name },
headers: { ...withAgentTargetHeader(nodeName) },
}
);
} catch (e) {
throw parseAxiosError(e, 'Failed renaming container');
}
await axios.post<void>(
urlBuilder(endpointId, id, 'rename'),
{},
{ params: { name }, transformResponse: genericHandler, headers }
);
}
export async function removeContainer(
@ -156,18 +156,11 @@ export async function removeContainer(
}: { removeVolumes?: boolean; nodeName?: string } = {}
) {
try {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
const { data } = await axios.delete<null | { message: string }>(
urlBuilder(endpointId, containerId),
buildDockerProxyUrl(endpointId, 'containers', containerId),
{
params: { v: removeVolumes ? 1 : 0, force: true },
transformResponse: genericHandler,
headers,
headers: { ...withAgentTargetHeader(nodeName) },
}
);
@ -175,24 +168,25 @@ export async function removeContainer(
throw new PortainerError(data.message);
}
} catch (e) {
throw new PortainerError('Unable to remove container', e as Error);
throw parseAxiosError(e, 'Unable to remove container');
}
}
export function urlBuilder(
endpointId: EnvironmentId,
id?: ContainerId,
action?: string
) {
let url = `/endpoints/${endpointId}/docker/containers`;
export async function getContainerLogs(
environmentId: EnvironmentId,
containerId: ContainerId,
params?: ContainerLogsParams
): Promise<string> {
try {
const { data } = await axios.get<string>(
buildDockerProxyUrl(environmentId, 'containers', containerId, 'logs'),
{
params: _.pickBy(params),
}
);
if (id) {
url += `/${id}`;
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to get container logs');
}
if (action) {
url += `/${action}`;
}
return url;
}

View file

@ -7,7 +7,6 @@ import {
MountPoint,
NetworkSettings,
} from 'docker-types/generated/1.41';
import { RawAxiosRequestHeaders } from 'axios';
import { PortainerResponse } from '@/react/docker/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
@ -15,11 +14,15 @@ import { ContainerId } from '@/react/docker/containers/types';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { queryClient } from '@/react-tools/react-query';
import { urlBuilder } from '../containers.service';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { queryKeys } from './query-keys';
export interface ContainerJSON {
/**
* Raw Docker Container Details response
*/
export interface ContainerDetailsJSON {
/**
* The ID of the container
*/
@ -83,7 +86,7 @@ export function useContainer(
containerId ? queryKeys.container(environmentId, containerId) : [],
() =>
containerId
? getContainer(environmentId, containerId, nodeName)
? getContainer(environmentId, containerId, { nodeName })
: undefined,
{
meta: {
@ -104,23 +107,28 @@ export function invalidateContainer(
);
}
export type ContainerResponse = PortainerResponse<ContainerJSON>;
export type ContainerDetailsResponse = PortainerResponse<ContainerDetailsJSON>;
async function getContainer(
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @param param2
* @returns
*/
export async function getContainer(
environmentId: EnvironmentId,
containerId: ContainerId,
nodeName?: string
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
try {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
const { data } = await axios.get<ContainerResponse>(
urlBuilder(environmentId, containerId, 'json'),
{ headers }
const { data } = await axios.get<ContainerDetailsResponse>(
buildDockerProxyUrl(environmentId, 'containers', id, 'json'),
{
headers: {
...withAgentTargetHeader(nodeName),
},
}
);
return data;
} catch (error) {

View file

@ -0,0 +1,38 @@
import { useQuery } from 'react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { ContainerId } from '../types';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { queryKeys } from './query-keys';
import { ContainerDetailsJSON } from './useContainer';
export function useContainerInspect(
environmentId: EnvironmentId,
id: ContainerId,
params: { nodeName?: string } = {}
) {
return useQuery({
queryKey: [...queryKeys.container(environmentId, id), params] as const,
queryFn: () => inspectContainer(environmentId, id, params),
});
}
export async function inspectContainer(
environmentId: EnvironmentId,
id: ContainerId,
{ nodeName }: { nodeName?: string } = {}
) {
try {
const { data } = await axios.get<ContainerDetailsJSON>(
buildDockerProxyUrl(environmentId, 'containers', id, 'json'),
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Failed inspecting container');
}
}

View file

@ -0,0 +1,27 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id exec instance id
*/
export async function resizeTTY(
environmentId: EnvironmentId,
id: string,
{ width, height }: { width: number; height: number }
) {
try {
await axios.post(
buildDockerProxyUrl(environmentId, 'containers', id, 'resize'),
{},
{
params: { h: height, w: width },
}
);
} catch (err) {
throw parseAxiosError(err, 'Unable to resize tty of container');
}
}

View file

@ -0,0 +1,136 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { ContainerId } from '../types';
/**
* This type is arbitrary and only defined based on what we use / observed from the API responses.
*/
export type ContainerStats = {
name?: string;
id?: string;
read?: string;
preread?: string;
pids_stats?: {
current?: number;
limit?: number;
};
memory_stats?: MemoryStats;
num_procs?: number;
precpu_stats?: CpuStats;
cpu_stats?: CpuStats;
networks?: Record<string, NetworkStats>;
blkio_stats?: BlkioStats;
storage_stats?: unknown;
};
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @returns
*/
export async function containerStats(
environmentId: EnvironmentId,
id: ContainerId
) {
try {
const { data } = await axios.get(
buildDockerProxyUrl(environmentId, 'containers', id, 'stats'),
{ params: { stream: false } }
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to retrieve container stats');
}
}
type BlkioStats = {
io_service_bytes_recursive?: {
major: number;
minor: number;
op: string;
value: number;
}[];
io_serviced_recursive?: null;
io_queue_recursive?: null;
io_service_time_recursive?: null;
io_wait_time_recursive?: null;
io_merged_recursive?: null;
io_time_recursive?: null;
sectors_recursive?: null;
};
type NetworkStats = {
rx_bytes?: number;
rx_packets?: number;
rx_errors?: number;
rx_dropped?: number;
tx_bytes?: number;
tx_packets?: number;
tx_errors?: number;
tx_dropped?: number;
};
type MemoryStats = {
privateworkingset?: number;
usage?: number;
stats?: MemoryStatsStats;
limit?: number;
};
type MemoryStatsStats = {
active_anon?: number;
active_file?: number;
anon?: number;
anon_thp?: number;
cache?: number;
file?: number;
file_dirty?: number;
file_mapped?: number;
file_writeback?: number;
inactive_anon?: number;
inactive_file?: number;
kernel_stack?: number;
pgactivate?: number;
pgdeactivate?: number;
pgfault?: number;
pglazyfree?: number;
pglazyfreed?: number;
pgmajfault?: number;
pgrefill?: number;
pgscan?: number;
pgsteal?: number;
shmem?: number;
slab?: number;
slab_reclaimable?: number;
slab_unreclaimable?: number;
sock?: number;
thp_collapse_alloc?: number;
thp_fault_alloc?: number;
unevictable?: number;
workingset_activate?: number;
workingset_nodereclaim?: number;
workingset_refault?: number;
};
type CpuUsage = {
total_usage?: number;
usage_in_kernelmode?: number;
usage_in_usermode?: number;
percpu_usage?: number[];
};
type ThrottlingData = {
periods?: number;
throttled_periods?: number;
throttled_time?: number;
};
type CpuStats = {
cpu_usage?: CpuUsage;
system_cpu_usage?: number;
online_cpus?: number;
throttling_data?: ThrottlingData;
};

View file

@ -0,0 +1,25 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { ContainerId } from '../types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @returns
*/
export async function containerTop(
environmentId: EnvironmentId,
id: ContainerId
) {
try {
const { data } = await axios.get(
buildDockerProxyUrl(environmentId, 'containers', id, 'top')
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to retrieve container top');
}
}

View file

@ -1,16 +1,17 @@
import { useQuery } from 'react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, {
agentTargetHeader,
parseAxiosError,
} from '@/portainer/services/axios';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { withGlobalError } from '@/react-tools/react-query';
import { urlBuilder } from '../containers.service';
import { DockerContainerResponse } from '../types/response';
import { toListViewModel } from '../utils';
import { DockerContainer } from '../types';
import { ContainerListViewModel } from '../types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import {
withFiltersQueryParam,
withAgentTargetHeader,
} from '../../proxy/queries/utils';
import { Filters } from './types';
import { queryKeys } from './query-keys';
@ -21,7 +22,7 @@ interface UseContainers {
nodeName?: string;
}
export function useContainers<T = DockerContainer[]>(
export function useContainers<T = ContainerListViewModel[]>(
environmentId: EnvironmentId,
{
autoRefreshRate,
@ -30,7 +31,7 @@ export function useContainers<T = DockerContainer[]>(
...params
}: UseContainers & {
autoRefreshRate?: number;
select?: (data: DockerContainer[]) => T;
select?: (data: ContainerListViewModel[]) => T;
enabled?: boolean;
} = {}
) {
@ -39,29 +40,29 @@ export function useContainers<T = DockerContainer[]>(
() => getContainers(environmentId, params),
{
...withGlobalError('Unable to retrieve containers'),
refetchInterval() {
return autoRefreshRate ?? false;
},
refetchInterval: autoRefreshRate ?? false,
select,
enabled,
}
);
}
/**
* Fetch containers and transform to ContainerListViewModel
* @param environmentId
* @param param1
* @returns ContainerListViewModel[]
*/
export async function getContainers(
environmentId: EnvironmentId,
{ all = true, filters, nodeName }: UseContainers = {}
) {
try {
const { data } = await axios.get<DockerContainerResponse[]>(
urlBuilder(environmentId, undefined, 'json'),
buildDockerProxyUrl(environmentId, 'containers', 'json'),
{
params: { all, filters: filters && JSON.stringify(filters) },
headers: nodeName
? {
[agentTargetHeader]: nodeName,
}
: undefined,
params: { all, ...withFiltersQueryParam(filters) },
headers: { ...withAgentTargetHeader(nodeName) },
}
);
return data.map((c) => toListViewModel(c));

View file

@ -0,0 +1,33 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { ContainerId } from '../types';
type ExecConfig = {
AttachStdin: boolean; // Attach to stdin of the exec command.
AttachStdout: boolean; // Attach to stdout of the exec command.
AttachStderr: boolean; // Attach to stderr of the exec command.
DetachKeys: string; // Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.
Tty: boolean; // Allocate a pseudo-TTY.
Env: string[]; // A list of environment variables in the form ["VAR=value", ...].
Cmd: string[]; // Command to run, as a string or array of strings.
Privileged: boolean; // Default: false - Runs the exec process with extended privileges.
User: string; // The user, and optionally, group to run the exec process inside the container. Format is one of: user, user:group, uid, or uid:gid.
WorkingDir: string; // The working directory for the exec process inside the container.
};
export async function createExec(
environmentId: EnvironmentId,
id: ContainerId,
config: ExecConfig
) {
try {
const { data } = await axios.post<{ Id: string }>(
buildDockerProxyUrl(environmentId, 'containers', id, 'exec'),
config
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to create exec');
}
}

View file

@ -1,10 +1,10 @@
import { Resources, RestartPolicy } from 'docker-types/generated/1.41';
import { RawAxiosRequestHeaders } from 'axios';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { urlBuilder } from '../containers.service';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
/**
* UpdateConfig holds the mutable attributes of a Container.
@ -12,27 +12,23 @@ import { urlBuilder } from '../containers.service';
*/
interface UpdateConfig extends Resources {
// Contains container's resources (cgroups, ulimits)
RestartPolicy?: RestartPolicy;
}
/**
* Raw docker API proxy
*/
export async function updateContainer(
environmentId: EnvironmentId,
containerId: string,
config: UpdateConfig,
{ nodeName }: { nodeName?: string } = {}
) {
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
try {
await axios.post<{ Warnings: string[] }>(
urlBuilder(environmentId, containerId, 'update'),
buildDockerProxyUrl(environmentId, 'containers', containerId, 'update'),
config,
{ headers }
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (err) {
throw parseAxiosError(err, 'failed updating container');

View file

@ -24,6 +24,9 @@ export interface Port {
export type ContainerId = string;
/**
* Computed fields from Container List Raw data
*/
type DecoratedDockerContainer = {
NodeName: string;
ResourceControl?: ResourceControlViewModel;
@ -32,9 +35,23 @@ type DecoratedDockerContainer = {
Status: ContainerStatus;
Ports: Port[];
StatusText: string;
Image: string;
Gpus: string;
};
export type DockerContainer = DecoratedDockerContainer &
/**
* Docker Container list ViewModel
*
* Alias AngularJS ContainerViewModel
*
* Raw details is ContainerDetailsJSON
*/
export type ContainerListViewModel = DecoratedDockerContainer &
Omit<DockerContainerResponse, keyof DecoratedDockerContainer>;
export type ContainerLogsParams = {
stdout?: boolean;
stderr?: boolean;
timestamps?: boolean;
since?: number;
tail?: number;
};

View file

@ -1,14 +1,11 @@
import {
EndpointSettings,
MountPoint,
Port,
} from 'docker-types/generated/1.41';
import { ContainerSummary } from 'docker-types/generated/1.41';
import { PortainerMetadata } from '@/react/docker/types';
import { PortainerResponse } from '@/react/docker/types';
import { WithRequiredProperties } from '@/types';
export interface SummaryNetworkSettings {
Networks: { [key: string]: EndpointSettings | undefined };
}
export type SummaryNetworkSettings = NonNullable<
ContainerSummary['NetworkSettings']
>;
export interface Health {
Status: 'healthy' | 'unhealthy' | 'starting';
@ -16,24 +13,23 @@ export interface Health {
Log: Array<{ Output: string }>;
}
export interface DockerContainerResponse {
Id: string;
Names: string[];
Image: string;
ImageID: string;
Command: string;
Created: number;
Ports: Port[];
SizeRw?: number;
SizeRootFs?: number;
Labels: { [key: string]: string };
State: string;
Status: string;
HostConfig: {
NetworkMode?: string;
};
NetworkSettings?: SummaryNetworkSettings;
Mounts: MountPoint[];
Portainer: PortainerMetadata;
IsPortainer: boolean;
}
/**
* Raw container list response item
*/
export type DockerContainerResponse = PortainerResponse<
WithRequiredProperties<
ContainerSummary,
| 'Id'
| 'Names'
| 'Image'
| 'ImageID'
| 'Command'
| 'Created'
| 'Ports'
| 'Labels'
| 'State'
| 'Status'
| 'HostConfig'
| 'Mounts'
>
>;

View file

@ -5,12 +5,17 @@ import { EnvironmentId } from '@/react/portainer/environments/types';
import { useInfo } from '@/react/docker/proxy/queries/useInfo';
import { useEnvironment } from '@/react/portainer/environments/queries';
import { DockerContainer, ContainerStatus } from './types';
import { ContainerListViewModel, ContainerStatus } from './types';
import { DockerContainerResponse } from './types/response';
/**
* Transform an item of the raw docker container list reponse to a container list view model
* @param response Raw docker container list reponse item
* @returns ContainerListViewModel
*/
export function toListViewModel(
response: DockerContainerResponse
): DockerContainer {
): ContainerListViewModel {
const resourceControl =
response.Portainer?.ResourceControl &&
new ResourceControlViewModel(response?.Portainer?.ResourceControl);

View file

@ -1,23 +0,0 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl as buildDockerUrl } from '@/react/docker/queries/utils/build-url';
import { buildUrl as buildDockerProxyUrl } from '@/react/docker/proxy/queries/build-url';
export function buildUrl(environmentId: EnvironmentId) {
return buildDockerUrl(environmentId, 'images');
}
export function buildProxyUrl(
environmentId: EnvironmentId,
{ id, action }: { id?: string; action?: string } = {}
) {
let dockerAction = '';
if (id) {
dockerAction += `${id}`;
}
if (action) {
dockerAction = dockerAction ? `${dockerAction}/${action}` : action;
}
return buildDockerProxyUrl(environmentId, 'images', dockerAction);
}

View file

@ -1,14 +0,0 @@
import { Registry } from '@/react/portainer/registries/types/registry';
/**
* Encodes the registry credentials in base64
* @param registryId
* @returns
*/
export function encodeRegistryCredentials(registryId: Registry['Id']) {
const credentials = {
registryId,
};
return window.btoa(JSON.stringify(credentials));
}

View file

@ -0,0 +1,285 @@
import axios, {
jsonObjectsToArrayHandler,
parseAxiosError,
} from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { formatArrayQueryParamsForDockerAPI } from '../../proxy/queries/utils';
export async function buildImageFromUpload(
environmentId: EnvironmentId,
names: string[],
file: File,
path: string
) {
return buildImage(
environmentId,
{ t: names, dockerfile: path },
file,
file.type
);
}
export async function buildImageFromURL(
environmentId: EnvironmentId,
names: string[],
url: string,
path: string
) {
return buildImage(
environmentId,
{ t: names, remote: url, dockerfile: path },
{},
'application/x-tar'
);
}
export async function buildImageFromDockerfileContent(
environmentId: EnvironmentId,
names: string[],
content: string
) {
return buildImage(
environmentId,
{ t: names },
{ content },
'application/json'
);
}
export async function buildImageFromDockerfileContentAndFiles(
environmentId: EnvironmentId,
names: string[],
content: string,
files: File[]
) {
const dockerfile = new Blob([content], { type: 'text/plain' });
const uploadFiles = [dockerfile, ...files];
return buildImage(
environmentId,
{ t: names },
{ file: uploadFiles },
'multipart/form-data'
);
}
/**
* Raw docker API proxy
*
* -----
*
* See api/http/proxy/factory/docker/build.go for the rules (copied below)
*
* buildOperation inspects the "Content-Type" header to determine if it needs to alter the request.
*
* -- buildImageFromUpload()
* If the value of the header is empty, it means that a Dockerfile is posted via upload, the function
* will extract the file content from the request body, tar it, and rewrite the body.
* !! THIS IS ONLY TRUE WHEN THE UPLOADED DOCKERFILE FILE HAS NO EXTENSION (the generated file.type in the frontend will be empty)
* If the Dockerfile is named like Dockerfile.yaml or has an internal type, a non-empty Content-Type header will be generated
*
* -- buildImageFromDockerfileContent()
* If the value of the header contains "application/json", it means that the content of a Dockerfile is posted
* in the request payload as JSON, the function will create a new file called Dockerfile inside a tar archive and
* rewrite the body of the request.
*
* -- buildImageFromUpload()
* -- buildImageFromURL()
* -- buildImageFromDockerfileContentAndFiles()
* In any other case, it will leave the request unaltered.
*
* -----
*
* @param environmentId
* @param params
* @param payload
* @param contentType
*/
async function buildImage(
environmentId: EnvironmentId,
params: BuildImageQueryParams,
payload: unknown,
contentType: string
) {
try {
const { data } = await axios.post(
buildDockerProxyUrl(environmentId, 'build'),
payload,
{
headers: { 'Content-Type': contentType },
params,
transformResponse: jsonObjectsToArrayHandler,
paramsSerializer: formatArrayQueryParamsForDockerAPI,
}
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to build image');
}
}
type BuildImageQueryParams = {
/**
* Path within the build context to the Dockerfile.
* This is ignored if remote is specified and points to an external Dockerfile.
*
* @default "Dockerfile"
*/
dockerfile?: string;
/**
* A name and optional tag to apply to the image in the name:tag format.
* If you omit the tag the default latest value is assumed.
* You can provide several t parameters.
*/
t?: string[];
/**
* Extra hosts to add to /etc/hosts
*/
extrahost?: string;
/**
* A Git repository URI or HTTP/HTTPS context URI.
* If the URI points to a single text file, the files contents are placed into a file called Dockerfile and the image is built from that file.
* If the URI points to a tarball, the file is downloaded by the daemon and the contents therein used as the context for the build.
* If the URI points to a tarball and the dockerfile parameter is also specified, there must be a file with the corresponding path inside the tarball.
*/
remote?: string;
/**
* Suppress verbose build output.
*
* @default false
*/
q?: boolean;
/**
* Do not use the cache when building the image.
*
* @default false
*/
nocache?: boolean;
/**
* JSON array of images used for build cache resolution.
*/
cachefrom?: string[];
/**
* Attempt to pull the image even if an older image exists locally.
*/
pull?: string;
/**
* Remove intermediate containers after a successful build.
*
* @default true
*/
rm?: boolean;
/**
* Always remove intermediate containers, even upon failure.
*
* @default false
*/
forcerm?: boolean;
/**
* Set memory limit for build.
*/
memory?: number;
/**
* Total memory (memory + swap).
*
* Set as -1 to disable swap.
*/
memswap?: number;
/**
* CPU shares (relative weight).
*/
cpushares?: number;
/**
* CPUs in which to allow execution (e.g., 0-3, 0,1).
*/
cpusetcpus?: string;
/**
* The length of a CPU period in microseconds.
*/
cpuperiod?: number;
/**
* Microseconds of CPU time that the container can get in a CPU period.
*/
cpuquota?: number;
/**
* JSON map of string pairs for build-time variables. Users pass these values at build-time.
* Docker uses the buildargs as the environment context for commands run via the Dockerfile RUN instruction, or for variable expansion in other Dockerfile instructions.
* This is not meant for passing secret values.
* For example, the build arg FOO=bar would become {"FOO":"bar"} in JSON. This would result in the query parameter buildargs={"FOO":"bar"}.
* Note that {"FOO":"bar"} should be URI component encoded.
* Read more about the buildargs instruction.
*/
buildargs?: string;
/**
* Size of /dev/shm in bytes. The size must be greater than 0. If omitted the system uses 64MB.
*/
shmsize?: number;
/**
* Squash the resulting images layers into a single layer. (Experimental release only.)
*/
squash?: boolean;
/**
* Arbitrary key/value labels to set on the image, as a JSON map of string pairs.
*/
labels?: Record<string, string>;
/**
* Sets the networking mode for the run commands during build. Supported standard values are: bridge, host, none, and container:<name|id>.
* Any other value is taken as a custom network's name or ID to which this container should connect to.
*/
networkmode?: string;
/**
* Platform in the format os[/arch[/variant]]
*
* @default ""
*/
platform?: string;
/**
* Target build stage
*
* @default ""
*/
target?: string;
/**
* BuildKit output configuration
*
* @default ""
*/
outputs?: string;
/**
* Version of the builder backend to use.
*
* @enum {('1' | '2')}
*
* @default '1'
*
* - 1 is the first generation classic (deprecated) builder in the Docker daemon (default)
* - 2 is BuildKit
*/
version?: string;
};

View file

@ -3,7 +3,8 @@ import { useQuery } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl } from './build-url';
import { buildDockerUrl } from '../../queries/utils/buildDockerUrl';
import { queryKeys } from './queryKeys';
export interface ImagesListResponse {
@ -20,6 +21,11 @@ export interface ImagesListResponse {
used: boolean;
}
/**
* Used in ImagesDatatable
*
* Query /api/docker/{envId}/images
*/
export function useImages<T = Array<ImagesListResponse>>(
environmentId: EnvironmentId,
withUsage = false,
@ -46,7 +52,7 @@ async function getImages(
) {
try {
const { data } = await axios.get<Array<ImagesListResponse>>(
buildUrl(environmentId),
buildDockerUrl(environmentId, 'images'),
{ params: { withUsage } }
);
return data;

View file

@ -1,13 +1,13 @@
import { RawAxiosRequestHeaders } from 'axios';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { Registry } from '@/react/portainer/registries/types/registry';
import { buildImageFullURI } from '../utils';
import { encodeRegistryCredentials } from './encodeRegistryCredentials';
import { buildProxyUrl } from './build-url';
import {
withRegistryAuthHeader,
withAgentTargetHeader,
} from '../../proxy/queries/utils';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
interface PullImageOptions {
environmentId: EnvironmentId;
@ -24,33 +24,27 @@ export async function pullImage({
nodeName,
registry,
}: PullImageOptions) {
const authenticationDetails =
registry && registry.Authentication
? encodeRegistryCredentials(registry.Id)
: '';
const imageURI = buildImageFullURI(image, registry);
const headers: RawAxiosRequestHeaders = {
'X-Registry-Auth': authenticationDetails,
};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
try {
await axios.post(buildProxyUrl(environmentId, { action: 'create' }), null, {
params: {
fromImage: imageURI,
},
headers,
});
await axios.post(
buildDockerProxyUrl(environmentId, 'images', 'create'),
null,
{
params: {
fromImage: imageURI,
},
headers: {
...withRegistryAuthHeader(registry?.Id),
...withAgentTargetHeader(nodeName),
},
}
);
} catch (err) {
if (ignoreErrors) {
return;
}
throw parseAxiosError(err as Error, 'Unable to pull image');
throw parseAxiosError(err, 'Unable to pull image');
}
}

View file

@ -0,0 +1,42 @@
import axios, {
jsonObjectsToArrayHandler,
parseAxiosError,
} from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { Registry } from '@/react/portainer/registries/types/registry';
import { buildImageFullURI } from '../utils';
import { withRegistryAuthHeader } from '../../proxy/queries/utils';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
interface PushImageOptions {
environmentId: EnvironmentId;
image: string;
registry?: Registry;
}
export async function pushImage({
environmentId,
image,
registry,
}: PushImageOptions) {
const imageURI = buildImageFullURI(image, registry);
try {
const { data } = await axios.post(
buildDockerProxyUrl(environmentId, 'images', imageURI, 'push'),
null,
{
headers: {
...withRegistryAuthHeader(registry?.Id),
},
transformResponse: jsonObjectsToArrayHandler,
}
);
if (data[data.length - 1].error) {
throw new Error(data[data.length - 1].error);
}
} catch (err) {
throw parseAxiosError(err, 'Unable to push image');
}
}

View file

@ -1,6 +1,6 @@
import { PortainerMetadata } from '../../types';
import { PortainerResponse } from '../../types';
export type DockerImageResponse = {
export type DockerImageResponse = PortainerResponse<{
Containers: number;
Created: number;
Id: string;
@ -10,5 +10,4 @@ export type DockerImageResponse = {
RepoTags: string[];
SharedSize: number;
Size: number;
Portainer?: PortainerMetadata;
};
}>;

View file

@ -4,16 +4,19 @@ import { useQueryClient } from 'react-query';
import { useEnvironmentId } from '@/react/hooks/useEnvironmentId';
import { AccessControlPanel } from '@/react/portainer/access-control/AccessControlPanel/AccessControlPanel';
import { ResourceControlType } from '@/react/portainer/access-control/types';
import { DockerContainer } from '@/react/docker/containers/types';
import { ContainerListViewModel } from '@/react/docker/containers/types';
import { ResourceControlViewModel } from '@/react/portainer/access-control/models/ResourceControlViewModel';
import { useContainers } from '@/react/docker/containers/queries/containers';
import { useContainers } from '@/react/docker/containers/queries/useContainers';
import { notifySuccess } from '@/portainer/services/notifications';
import { confirmDelete } from '@@/modals/confirm';
import { PageHeader } from '@@/PageHeader';
import { useNetwork, useDeleteNetwork } from '../queries';
import { useDeleteNetwork } from '../queries/useDeleteNetworkMutation';
import { isSystemNetwork } from '../network.helper';
import { NetworkResponseContainers } from '../types';
import { queryKeys } from '../queries/queryKeys';
import { useNetwork } from '../queries/useNetwork';
import { NetworkDetailsTable } from './NetworkDetailsTable';
import { NetworkOptionsTable } from './NetworkOptionsTable';
@ -28,7 +31,7 @@ export function ItemView() {
} = useCurrentStateAndParams();
const environmentId = useEnvironmentId();
const networkQuery = useNetwork(environmentId, networkId, { nodeName });
const deleteNetworkMutation = useDeleteNetwork();
const deleteNetworkMutation = useDeleteNetwork(environmentId);
const containersQuery = useContainers(environmentId, {
filters: {
network: [networkId],
@ -70,13 +73,9 @@ export function ItemView() {
<AccessControlPanel
onUpdateSuccess={() =>
queryClient.invalidateQueries([
'environments',
environmentId,
'docker',
'networks',
networkId,
])
queryClient.invalidateQueries(
queryKeys.item(environmentId, networkId)
)
}
resourceControl={resourceControl}
resourceType={ResourceControlType.Network}
@ -100,9 +99,10 @@ export function ItemView() {
if (confirmed) {
deleteNetworkMutation.mutate(
{ environmentId, networkId, nodeName },
{ networkId, nodeName },
{
onSuccess: () => {
notifySuccess('Network successfully removed', networkId);
router.stateService.go('docker.networks');
},
}
@ -113,7 +113,7 @@ export function ItemView() {
function filterContainersInNetwork(
networkContainers?: NetworkResponseContainers,
containers: DockerContainer[] = []
containers: ContainerListViewModel[] = []
) {
if (!networkContainers) {
return [];

View file

@ -3,6 +3,7 @@ import { Server, Trash2 } from 'lucide-react';
import { Authorized } from '@/react/hooks/useUser';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { Icon } from '@/react/components/Icon';
import { notifySuccess } from '@/portainer/services/notifications';
import { TableContainer, TableTitle } from '@@/datatables';
import { DetailsTable } from '@@/DetailsTable';
@ -10,7 +11,7 @@ import { Button } from '@@/buttons';
import { Link } from '@@/Link';
import { NetworkContainer, NetworkId } from '../types';
import { useDisconnectContainer } from '../queries';
import { useDisconnectContainer } from '../queries/useDisconnectContainerMutation';
type Props = {
networkContainers: NetworkContainer[];
@ -33,7 +34,10 @@ export function NetworkContainersTable({
environmentId,
networkId,
}: Props) {
const disconnectContainer = useDisconnectContainer();
const disconnectContainer = useDisconnectContainer({
environmentId,
networkId,
});
if (networkContainers.length === 0) {
return null;
@ -71,12 +75,19 @@ export function NetworkContainersTable({
color="dangerlight"
onClick={() => {
if (container.Id) {
disconnectContainer.mutate({
containerId: container.Id,
environmentId,
networkId,
nodeName,
});
disconnectContainer.mutate(
{
containerId: container.Id,
nodeName,
},
{
onSuccess: () =>
notifySuccess(
'Container successfully disconnected',
networkId
),
}
);
}
}}
>

View file

@ -21,7 +21,7 @@ import { useIsSwarm } from '../../proxy/queries/useInfo';
import { useColumns } from './columns';
import { DecoratedNetwork } from './types';
import { NestedNetworksDatatable } from './NestedNetwordsTable';
import { NestedNetworksDatatable } from './NestedNetworksTable';
const storageKey = 'docker.networks';

View file

@ -1,7 +1,7 @@
import _ from 'lodash';
import { useMemo } from 'react';
import { createOwnershipColumn } from '@/react/docker/components/datatable/createOwnershipColumn';
import { createOwnershipColumn } from '@/react/docker/components/datatables/createOwnershipColumn';
import { buildExpandColumn } from '@@/datatables/expand-column';

View file

@ -1,102 +0,0 @@
import { ContainerId } from '@/react/docker/containers/types';
import axios, {
agentTargetHeader,
parseAxiosError,
} from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { NetworkId, DockerNetwork } from './types';
type NetworkAction = 'connect' | 'disconnect' | 'create';
export async function getNetwork(
environmentId: EnvironmentId,
networkId: NetworkId,
{ nodeName }: { nodeName?: string } = {}
) {
try {
const { data: network } = await axios.get<DockerNetwork>(
buildUrl(environmentId, networkId),
nodeName
? {
headers: {
[agentTargetHeader]: nodeName,
},
}
: undefined
);
return network;
} catch (e) {
throw parseAxiosError(e as Error, 'Unable to retrieve network details');
}
}
export async function deleteNetwork(
environmentId: EnvironmentId,
networkId: NetworkId,
{ nodeName }: { nodeName?: string } = {}
) {
try {
await axios.delete(
buildUrl(environmentId, networkId),
nodeName
? {
headers: {
[agentTargetHeader]: nodeName,
},
}
: undefined
);
return networkId;
} catch (e) {
throw parseAxiosError(e as Error, 'Unable to remove network');
}
}
export async function disconnectContainer(
environmentId: EnvironmentId,
networkId: NetworkId,
containerId: ContainerId,
nodeName?: string
) {
try {
await axios.post(
buildUrl(environmentId, networkId, 'disconnect'),
{
Container: containerId,
Force: false,
},
nodeName
? {
headers: {
[agentTargetHeader]: nodeName,
},
}
: undefined
);
return { networkId, environmentId };
} catch (e) {
throw parseAxiosError(
e as Error,
'Unable to disconnect container from network'
);
}
}
function buildUrl(
environmentId: EnvironmentId,
networkId?: NetworkId,
action?: NetworkAction
) {
let url = `endpoints/${environmentId}/docker/networks`;
if (networkId) {
url += `/${networkId}`;
}
if (action) {
url += `/${action}`;
}
return url;
}

View file

@ -1,97 +0,0 @@
import { useQuery, useMutation, useQueryClient } from 'react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import {
error as notifyError,
success as notifySuccess,
} from '@/portainer/services/notifications';
import { ContainerId } from '@/react/docker/containers/types';
import {
getNetwork,
deleteNetwork,
disconnectContainer,
} from './network.service';
import { NetworkId } from './types';
export function useNetwork(
environmentId: EnvironmentId,
networkId: NetworkId,
{ nodeName }: { nodeName?: string } = {}
) {
return useQuery(
[
'environments',
environmentId,
'docker',
'networks',
networkId,
{ nodeName },
],
() => getNetwork(environmentId, networkId, { nodeName }),
{
onError: (err) => {
notifyError('Failure', err as Error, 'Unable to get network');
},
}
);
}
export function useDeleteNetwork() {
return useMutation(
({
environmentId,
networkId,
nodeName,
}: {
environmentId: EnvironmentId;
networkId: NetworkId;
nodeName?: string;
}) => deleteNetwork(environmentId, networkId, { nodeName }),
{
onSuccess: (networkId) => {
notifySuccess('Network successfully removed', networkId);
},
onError: (err) => {
notifyError('Failure', err as Error, 'Unable to remove network');
},
}
);
}
export function useDisconnectContainer() {
const client = useQueryClient();
return useMutation(
({
containerId,
environmentId,
networkId,
nodeName,
}: {
containerId: ContainerId;
environmentId: EnvironmentId;
networkId: NetworkId;
nodeName?: string;
}) => disconnectContainer(environmentId, networkId, containerId, nodeName),
{
onSuccess: ({ networkId, environmentId }) => {
notifySuccess('Container successfully disconnected', networkId);
return client.invalidateQueries([
'environments',
environmentId,
'docker',
'networks',
networkId,
]);
},
onError: (err) => {
notifyError(
'Failure',
err as Error,
'Unable to disconnect container from network'
);
},
}
);
}

View file

@ -1,20 +0,0 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl as buildDockerUrl } from '../../proxy/queries/build-url';
import { NetworkId } from '../types';
export function buildUrl(
environmentId: EnvironmentId,
{ id, action }: { id?: NetworkId; action?: string } = {}
) {
let baseUrl = 'networks';
if (id) {
baseUrl += `/${id}`;
}
if (action) {
baseUrl += `/${action}`;
}
return buildDockerUrl(environmentId, baseUrl);
}

View file

@ -1,6 +1,7 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import { queryKeys as dockerQueryKeys } from '../../queries/utils';
import { NetworkId } from '../types';
import { NetworksQuery } from './types';
@ -9,4 +10,6 @@ export const queryKeys = {
[...dockerQueryKeys.root(environmentId), 'networks'] as const,
list: (environmentId: EnvironmentId, query: NetworksQuery) =>
[...queryKeys.base(environmentId), 'list', query] as const,
item: (environmentId: EnvironmentId, id: NetworkId) =>
[...queryKeys.base(environmentId), id] as const,
};

View file

@ -1,5 +1,4 @@
import { EndpointSettings } from 'docker-types/generated/1.41';
import { RawAxiosRequestHeaders } from 'axios';
import { useMutation, useQueryClient } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
@ -11,8 +10,8 @@ import {
} from '@/react-tools/react-query';
import { queryKeys as dockerQueryKeys } from '../../queries/utils';
import { buildUrl } from './buildUrl';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
interface ConnectContainerPayload {
Container: string;
@ -40,6 +39,9 @@ interface ConnectContainer {
nodeName?: string;
}
/**
* Raw docker API proxy
*/
export async function connectContainer({
environmentId,
containerId,
@ -56,16 +58,11 @@ export async function connectContainer({
};
}
const headers: RawAxiosRequestHeaders = {};
if (nodeName) {
headers['X-PortainerAgent-Target'] = nodeName;
}
try {
await axios.post(
buildUrl(environmentId, { id: networkId, action: 'connect' }),
payload
buildDockerProxyUrl(environmentId, 'networks', networkId, 'connect'),
payload,
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
} catch (err) {
throw parseAxiosError(err as Error, 'Unable to connect container');

View file

@ -0,0 +1,91 @@
import { Network } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import {
withAgentManagerOperationHeader,
withAgentTargetHeader,
} from '../../proxy/queries/utils';
type MacvlanConfigOnly = {
ConfigOnly: true;
Internal: false;
Attachable: false;
Options: {
parent: string; // parent network card
};
};
type MacvlanConfigFrom = {
ConfigFrom: {
Network: string;
};
Scope: 'swarm' | 'local';
};
type NetworkConfigBase = {
Name: Required<Network>['Name'];
CheckDuplicate?: boolean;
Driver?: string;
Internal?: boolean;
Attachable?: boolean;
Ingress?: boolean;
IPAM?: Network['IPAM'];
EnableIPv6?: boolean;
Options?: Network['Options'];
Labels?: Network['Labels'];
};
/**
* This type definition of NetworkConfig doesnt enforce the usage of only one type of the union
* and not a mix of fields of the unionised types.
* e.g. the following is valid for TS while it is not for the Docker API
*
* const config: NetworkConfig = {
* Name: 'my-network', // shared
* ConfigOnly: true, // MacvlanConfigOnly
* Scope: 'swarm', // MacvlanConfigFrom
* }
*
*/
type NetworkConfig =
| NetworkConfigBase
| (NetworkConfigBase & MacvlanConfigOnly)
| (NetworkConfigBase & MacvlanConfigFrom);
type CreateOptions = {
nodeName?: string;
agentManagerOperation?: boolean;
};
type CreateNetworkResponse = {
Id: string;
Warning: string;
};
/**
* Raw docker API proxy
*/
export async function createNetwork(
environmentId: EnvironmentId,
networkConfig: NetworkConfig,
{ nodeName, agentManagerOperation }: CreateOptions = {}
) {
try {
const { data } = await axios.post<CreateNetworkResponse>(
buildDockerProxyUrl(environmentId, 'networks', 'create'),
networkConfig,
{
headers: {
...withAgentTargetHeader(nodeName),
...withAgentManagerOperationHeader(agentManagerOperation),
},
}
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to create network');
}
}

View file

@ -0,0 +1,50 @@
import { useMutation, useQueryClient } from 'react-query';
import {
mutationOptions,
withError,
withInvalidate,
} from '@/react-tools/react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { NetworkId } from '../types';
import { queryKeys } from './queryKeys';
export function useDeleteNetwork(environmentId: EnvironmentId) {
const queryClient = useQueryClient();
return useMutation(
({ networkId, nodeName }: { networkId: NetworkId; nodeName?: string }) =>
deleteNetwork(environmentId, networkId, { nodeName }),
mutationOptions(
withInvalidate(queryClient, [queryKeys.base(environmentId)]),
withError('Unable to remove network')
)
);
}
/**
* Raw docker API proxy
* @param environmentId
* @param networkId
* @returns
*/
export async function deleteNetwork(
environmentId: EnvironmentId,
networkId: NetworkId,
{ nodeName }: { nodeName?: string } = {}
) {
try {
await axios.delete(
buildDockerProxyUrl(environmentId, 'networks', networkId),
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
return networkId;
} catch (err) {
throw parseAxiosError(err, 'Unable to remove network');
}
}

View file

@ -0,0 +1,68 @@
import { useMutation, useQueryClient } from 'react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import {
mutationOptions,
withError,
withInvalidate,
} from '@/react-tools/react-query';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { ContainerId } from '../../containers/types';
import { NetworkId } from '../types';
import { queryKeys } from './queryKeys';
export function useDisconnectContainer({
environmentId,
networkId,
}: {
environmentId: EnvironmentId;
networkId: NetworkId;
}) {
const client = useQueryClient();
return useMutation(
({
containerId,
nodeName,
}: {
containerId: ContainerId;
nodeName?: string;
}) => disconnectContainer(environmentId, networkId, containerId, nodeName),
mutationOptions(
withInvalidate(client, [queryKeys.item(environmentId, networkId)]),
withError('Unable to disconnect container from network')
)
);
}
/**
* Raw docker API proxy
* @param environmentId
* @param networkId
* @param containerId
* @returns
*/
export async function disconnectContainer(
environmentId: EnvironmentId,
networkId: NetworkId,
containerId: ContainerId,
nodeName?: string
) {
try {
await axios.post(
buildDockerProxyUrl(environmentId, 'networks', networkId, 'disconnect'),
{
Container: containerId,
Force: false,
},
{ headers: { ...withAgentTargetHeader(nodeName) } }
);
return { networkId, environmentId };
} catch (err) {
throw parseAxiosError(err, 'Unable to disconnect container from network');
}
}

View file

@ -0,0 +1,52 @@
import { useQuery } from 'react-query';
import { withGlobalError } from '@/react-tools/react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { DockerNetwork, NetworkId } from '../types';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { withAgentTargetHeader } from '../../proxy/queries/utils';
import { queryKeys } from './queryKeys';
export function useNetwork(
environmentId: EnvironmentId,
networkId: NetworkId,
{ nodeName }: { nodeName?: string } = {}
) {
return useQuery(
[...queryKeys.item(environmentId, networkId), { nodeName }],
() => getNetwork(environmentId, networkId, { nodeName }),
{
...withGlobalError('Unable to get network'),
}
);
}
/**
* Raw docker API proxy
* @param environmentId
* @param networkId
* @param param2
* @returns
*/
export async function getNetwork(
environmentId: EnvironmentId,
networkId: NetworkId,
{ nodeName }: { nodeName?: string } = {}
) {
try {
const { data: network } = await axios.get<DockerNetwork>(
buildDockerProxyUrl(environmentId, 'networks', networkId),
{
headers: {
...withAgentTargetHeader(nodeName),
},
}
);
return network;
} catch (e) {
throw parseAxiosError(e as Error, 'Unable to retrieve network details');
}
}

View file

@ -3,8 +3,9 @@ import { useQuery } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl } from '../../proxy/queries/build-url';
import { DockerNetwork } from '../types';
import { withFiltersQueryParam } from '../../proxy/queries/utils';
import { buildDockerProxyUrl } from '../../proxy/queries/buildDockerProxyUrl';
import { queryKeys } from './queryKeys';
import { NetworksQuery } from './types';
@ -29,17 +30,18 @@ export function useNetworks<T = Array<DockerNetwork>>(
);
}
/**
* Raw docker API proxy
*/
export async function getNetworks(
environmentId: EnvironmentId,
{ local, swarm, swarmAttachable, filters }: NetworksQuery
) {
try {
const { data } = await axios.get<Array<DockerNetwork>>(
buildUrl(environmentId, 'networks'),
filters && {
params: {
filters,
},
buildDockerProxyUrl(environmentId, 'networks'),
{
params: { ...withFiltersQueryParam(filters) },
}
);

View file

@ -1,6 +1,5 @@
import { PortainerMetadata } from '@/react/docker/types';
import { ContainerId } from '../containers/types';
import { PortainerResponse } from '@/react/docker/types';
import { ContainerId } from '@/react/docker/containers/types';
export type IPConfig = {
Subnet: string;
@ -32,7 +31,7 @@ export type NetworkResponseContainers = Record<
NetworkResponseContainer
>;
export interface DockerNetwork {
export interface DockerNetwork extends PortainerResponse<unknown> {
Name: string;
Id: NetworkId;
Driver: string;
@ -44,7 +43,6 @@ export interface DockerNetwork {
Driver: string;
Options: IpamOptions;
};
Portainer?: PortainerMetadata;
Options: NetworkOptions;
Containers: NetworkResponseContainers;
}

View file

@ -1,15 +0,0 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
export function buildUrl(
environmentId: EnvironmentId,
action: string,
subAction = ''
) {
let url = `/endpoints/${environmentId}/docker/${action}`;
if (subAction) {
url += `/${subAction}`;
}
return url;
}

View file

@ -0,0 +1,50 @@
import { compact } from 'lodash';
import { EnvironmentId } from '@/react/portainer/environments/types';
/**
* Build docker proxy URL for Environment
*
* @param environmentId
* @param action
* @param subSegments Sub segments are only added to the URL when they are not `undefined`.
* @returns `/endpoints/{environmentId}/docker/{action}/{subSegments[0]}/{subSegments[1]}/...`
*
* @example
* // all calls return /endpoints/1/docker/action/sub1/sub2
* buildDockerProxyUrl(1, 'action', 'sub1', 'sub2');
* buildDockerProxyUrl(1, 'action', undefined, 'sub1', undefined, 'sub2');
*
* @example
* function buildUrl(endpointId: EnvironmentId, id?: ServiceId, action?: string) {
* return buildDockerProxyUrl(endpointId, 'services', id, action);
*}
*
* // returns /endpoints/1/docker/services/ubx3r/update
* buildUrl(1, 'ubx3r', 'update')
*
* // returns /endpoints/1/docker/services/update
* buildUrl(1, undefined, 'update')
*
* // returns /endpoints/1/docker/services/ubx3r
* buildUrl(1, 'ubx3r') // = buildUrl(1, 'ubx3r', undefined)
*
* // returns /endpoints/1/docker/services
* buildUrl(1) // = buildUrl(1, undefined, undefined)
*
*/
export function buildDockerProxyUrl(
environmentId: EnvironmentId,
action: string,
...subSegments: unknown[]
) {
let url = `/endpoints/${environmentId}/docker/${action}`;
const joined = compact(subSegments).join('/');
if (joined) {
url += `/${joined}`;
}
return url;
}

View file

@ -0,0 +1,31 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
import { formatArrayQueryParamsForDockerAPI } from '../utils';
/**
* Raw docker API proxy
*/
export async function downloadImages(
environmentId: EnvironmentId,
images: { tags: string[]; id: string }[]
) {
const names = images.map((image) =>
image.tags[0] !== '<none>:<none>' ? image.tags[0] : image.id
);
try {
const { data } = await axios.get(
buildDockerProxyUrl(environmentId, 'images', 'get'),
{
params: { names },
responseType: 'blob',
paramsSerializer: formatArrayQueryParamsForDockerAPI,
}
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to download images');
}
}

View file

@ -0,0 +1,26 @@
import { ImageInspect } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @returns
*/
export async function getImage(
environmentId: EnvironmentId,
id: Required<ImageInspect['Id']>
) {
try {
const { data } = await axios.get<ImageInspect>(
buildDockerProxyUrl(environmentId, 'images', id, 'json')
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to retrieve image');
}
}

View file

@ -0,0 +1,32 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
export type ImageLayer = {
Id: string;
Created: number;
CreatedBy: string;
Tags: string[];
Size: number;
Comment: string;
};
/**
* Raw docker API proxy
* @param environmentId
* @returns
*/
export async function getImageHistory(
environmentId: EnvironmentId,
id: ImageLayer['Id']
) {
try {
const { data } = await axios.get<ImageLayer[]>(
buildDockerProxyUrl(environmentId, 'images', id, 'history')
);
return data;
} catch (err) {
throw parseAxiosError(err as Error, 'Unable to retrieve image layers');
}
}

View file

@ -4,7 +4,7 @@ import { ImageSummary } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl } from '../build-url';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
import { queryKeys } from './queryKeys';
@ -24,10 +24,15 @@ export function useImages<T = ImagesListResponse>(
);
}
async function getImages(environmentId: EnvironmentId) {
/**
* Raw docker API proxy
* @param environmentId
* @returns
*/
export async function getImages(environmentId: EnvironmentId) {
try {
const { data } = await axios.get<ImagesListResponse>(
buildUrl(environmentId, 'images', 'json')
buildDockerProxyUrl(environmentId, 'images', 'json')
);
return data;
} catch (err) {

View file

@ -0,0 +1,28 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { ImageId, ImageName } from '@/docker/models/image';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @param force
* @returns
*/
export async function removeImage(
environmentId: EnvironmentId,
id: ImageId | ImageName,
force?: boolean
) {
try {
const { data } = await axios.delete(
buildDockerProxyUrl(environmentId, 'images', id),
{ params: { force } }
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to remove image');
}
}

View file

@ -0,0 +1,29 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { ImageId, ImageName } from '@/docker/models/image';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @param repo string generated by `buildImageFullURIFromModel` or `buildImageFullURI`
* @returns
*/
export async function tagImage(
environmentId: EnvironmentId,
id: ImageId | ImageName,
repo: string
) {
try {
const { data } = await axios.post(
buildDockerProxyUrl(environmentId, 'images', id, 'tag'),
{},
{ params: { repo } }
);
return data;
} catch (e) {
throw parseAxiosError(e, 'Unable to tag image');
}
}

View file

@ -0,0 +1,26 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param file
* @returns
*/
export async function uploadImages(environmentId: EnvironmentId, file: File) {
try {
return await axios.post(
buildDockerProxyUrl(environmentId, 'images', 'load'),
file,
{
headers: {
'Content-Type': file.type, // 'application/x-tar',
},
}
);
} catch (e) {
throw parseAxiosError(e, 'Unable to upload image');
}
}

View file

@ -1,15 +0,0 @@
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl as buildProxyUrl } from '../build-url';
export function buildUrl(
environmentId: EnvironmentId,
action?: string,
subAction = ''
) {
return buildProxyUrl(
environmentId,
'nodes',
subAction ? `${action}/${subAction}` : action
);
}

View file

@ -0,0 +1,26 @@
import { Node } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @returns
*/
export async function getNode(
environmentId: EnvironmentId,
id: NonNullable<Node['ID']>
) {
try {
const { data } = await axios.get<Node>(
buildDockerProxyUrl(environmentId, 'nodes', id)
);
return data;
} catch (error) {
throw parseAxiosError(error, 'Unable to retrieve node');
}
}

View file

@ -4,16 +4,24 @@ import { useQuery } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl } from './build-url';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
import { queryKeys } from './query-keys';
export function useNodes(environmentId: EnvironmentId) {
return useQuery(queryKeys.base(environmentId), () => getNodes(environmentId));
}
async function getNodes(environmentId: EnvironmentId) {
/**
* Raw docker API proxy
* @param environmentId
* @returns
*/
export async function getNodes(environmentId: EnvironmentId) {
try {
const { data } = await axios.get<Array<Node>>(buildUrl(environmentId));
const { data } = await axios.get<Array<Node>>(
buildDockerProxyUrl(environmentId, 'nodes')
);
return data;
} catch (error) {
throw parseAxiosError(error, 'Unable to retrieve nodes');

View file

@ -0,0 +1,31 @@
import { Node, NodeSpec } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id
* @param node
* @param version
*/
export async function updateNode(
environmentId: EnvironmentId,
id: NonNullable<Node['ID']>,
node: NodeSpec,
version: number
) {
try {
const { data } = await axios.post(
buildDockerProxyUrl(environmentId, 'nodes', id, 'update'),
node,
{ params: { version } }
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to update node');
}
}

View file

@ -0,0 +1,21 @@
import { SecretSpec } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
export async function createSecret(
environmentId: EnvironmentId,
secret: SecretSpec
) {
try {
const { data } = await axios.post(
buildDockerProxyUrl(environmentId, 'secrets', 'create'),
secret
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to create secret');
}
}

View file

@ -0,0 +1,17 @@
import { Secret } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
export async function removeSecret(
environmentId: EnvironmentId,
id: NonNullable<Secret['ID']>
) {
try {
await axios.delete(buildDockerProxyUrl(environmentId, 'secrets', id));
} catch (err) {
throw parseAxiosError(err, 'Unable to remove secret');
}
}

View file

@ -0,0 +1,21 @@
import { Secret } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { PortainerResponse } from '@/react/docker/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
export async function getSecret(
environmentId: EnvironmentId,
id: NonNullable<Secret['ID']>
) {
try {
const { data } = await axios.get<PortainerResponse<Secret>>(
buildDockerProxyUrl(environmentId, 'secrets', id)
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to retrieve secret');
}
}

View file

@ -0,0 +1,15 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
export async function getSecrets(environmentId: EnvironmentId) {
try {
const { data } = await axios.get(
buildDockerProxyUrl(environmentId, 'secrets')
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to retrieve secrets');
}
}

View file

@ -0,0 +1,35 @@
import { Task } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from '../buildDockerProxyUrl';
import { withFiltersQueryParam } from '../utils';
type Filters = {
'desired-state'?: 'running' | 'shutdown' | 'accepted';
id?: Task['ID'];
label?: Task['Labels'];
name?: Task['Name'];
node?: Task['NodeID'];
service?: Task['ServiceID'];
};
export async function getTasks(
environmentId: EnvironmentId,
filters?: Filters
) {
try {
const { data } = await axios.get<Task[]>(
buildDockerProxyUrl(environmentId, 'tasks'),
{
params: {
...withFiltersQueryParam(filters),
},
}
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to retrieve tasks');
}
}

View file

@ -0,0 +1,32 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
type CommitParams = {
container?: string; // The ID or name of the container to commit
repo?: string; // Repository name for the created image
tag?: string; // Tag name for the create image
comment?: string; // Commit message
author?: string; // Author of the image (e.g., John Hannibal Smith <hannibal@a-team.com>)
pause?: boolean; // Default: true Whether to pause the container before committing
changes?: string; // Dockerfile instructions to apply while committing
};
export async function commitContainer(
environmentId: EnvironmentId,
params: CommitParams
) {
try {
const { data } = await axios.post<{ Id: string }>(
buildDockerProxyUrl(environmentId, 'commit'),
{},
{
params,
}
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to commit container');
}
}

View file

@ -0,0 +1,30 @@
import { EventMessage } from 'docker-types/generated/1.41';
import axios, {
jsonObjectsToArrayHandler,
parseAxiosError,
} from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param param1
* @returns
*/
export async function getEvents(
environmentId: EnvironmentId,
{ since, until }: { since: string; until: string }
) {
try {
const { data } = await axios.get<EventMessage[]>(
buildDockerProxyUrl(environmentId, 'events'),
{ params: { since, until }, transformResponse: jsonObjectsToArrayHandler }
);
return data;
} catch (err) {
throw parseAxiosError(err, 'Unable to retrieve engine events');
}
}

View file

@ -0,0 +1,30 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
/**
* Raw docker API proxy
* @param environmentId
* @param id exec instance id
*/
export async function resizeTTY(
environmentId: EnvironmentId,
id: string,
{ width, height }: { width: number; height: number }
) {
try {
await axios.post(
buildDockerProxyUrl(environmentId, 'exec', id, 'resize'),
{},
{
params: {
h: height,
w: width,
},
}
);
} catch (err) {
throw parseAxiosError(err, 'Unable to resize tty of exec');
}
}

View file

@ -4,16 +4,16 @@ import { SystemInfo } from 'docker-types/generated/1.41';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildUrl } from './build-url';
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
export async function getInfo(environmentId: EnvironmentId) {
try {
const { data } = await axios.get<SystemInfo>(
buildUrl(environmentId, 'info')
buildDockerProxyUrl(environmentId, 'info')
);
return data;
} catch (err) {
throw parseAxiosError(err as Error, 'Unable to retrieve version');
throw parseAxiosError(err, 'Unable to retrieve system info');
}
}

View file

@ -0,0 +1,12 @@
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
export async function ping(environmentId: EnvironmentId) {
try {
await axios.get(buildDockerProxyUrl(environmentId, '_ping'));
} catch (error) {
throw parseAxiosError(error);
}
}

View file

@ -10,13 +10,19 @@ import { EnvironmentId } from '@/react/portainer/environments/types';
import { queryKeys } from '../../queries/utils/root';
import { buildUrl } from './build-url';
import { buildDockerProxyUrl } from './buildDockerProxyUrl';
import { useInfo } from './useInfo';
const pluginTypeToVersionMap: { [k in keyof PluginsInfo]: string } = {
Volume: 'docker.volumedriver/1.0',
Network: 'docker.networkdriver/1.0',
Log: 'docker.logdriver/1.0',
};
export async function getPlugins(environmentId: EnvironmentId) {
try {
const { data } = await axios.get<Array<Plugin>>(
buildUrl(environmentId, 'plugins')
buildDockerProxyUrl(environmentId, 'plugins')
);
return data;
} catch (e) {
@ -38,77 +44,74 @@ function usePlugins(
export function useServicePlugins(
environmentId: EnvironmentId,
systemOnly: boolean,
pluginType: keyof PluginsInfo,
pluginVersion: string
pluginType: keyof PluginsInfo
) {
const systemPluginsQuery = useInfo(environmentId, (info) => info.Plugins);
const pluginsQuery = usePlugins(environmentId, { enabled: !systemOnly });
return {
data: aggregateData(),
data: aggregateData(
systemPluginsQuery.data,
pluginsQuery.data,
systemOnly,
pluginType
),
isLoading: systemPluginsQuery.isLoading || pluginsQuery.isLoading,
};
}
function aggregateData() {
if (!systemPluginsQuery.data) {
return null;
}
const systemPlugins = systemPluginsQuery.data[pluginType] || [];
if (systemOnly) {
return systemPlugins;
}
const plugins =
pluginsQuery.data
?.filter(
(plugin) =>
plugin.Enabled &&
// docker has an error in their types, so we need to cast to unknown first
// see https://docs.docker.com/engine/api/v1.41/#tag/Plugin/operation/PluginList
plugin.Config.Interface.Types.includes(
pluginVersion as unknown as PluginInterfaceType
)
)
.map((plugin) => plugin.Name) || [];
return [...systemPlugins, ...plugins];
/**
* @private Exported only for AngularJS `PluginService` factory `app/docker/services/pluginService.js`
*/
export function aggregateData(
systemPluginsData: PluginsInfo | undefined,
pluginsData: Plugin[] | undefined,
systemOnly: boolean,
pluginType: keyof PluginsInfo
) {
if (!systemPluginsData) {
return null;
}
const systemPlugins = systemPluginsData[pluginType] || [];
if (systemOnly) {
return systemPlugins;
}
const plugins =
pluginsData
?.filter(
(plugin) =>
plugin.Enabled &&
// docker has an error in their types, so we need to cast to unknown first
// see https://docs.docker.com/engine/api/v1.41/#tag/Plugin/operation/PluginList
plugin.Config.Interface.Types.includes(
pluginTypeToVersionMap[pluginType] as unknown as PluginInterfaceType
)
)
.map((plugin) => plugin.Name) || [];
return [...systemPlugins, ...plugins];
}
export function useLoggingPlugins(
environmentId: EnvironmentId,
systemOnly: boolean
) {
return useServicePlugins(
environmentId,
systemOnly,
'Log',
'docker.logdriver/1.0'
);
return useServicePlugins(environmentId, systemOnly, 'Log');
}
export function useVolumePlugins(
environmentId: EnvironmentId,
systemOnly: boolean
) {
return useServicePlugins(
environmentId,
systemOnly,
'Volume',
'docker.volumedriver/1.0'
);
return useServicePlugins(environmentId, systemOnly, 'Volume');
}
export function useNetworkPlugins(
environmentId: EnvironmentId,
systemOnly: boolean
) {
return useServicePlugins(
environmentId,
systemOnly,
'Network',
'docker.networkdriver/1.0'
);
return useServicePlugins(environmentId, systemOnly, 'Network');
}

Some files were not shown because too many files have changed in this diff Show more