2024-04-14 14:54:25 +00:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
2023-11-15 12:43:18 +00:00
|
|
|
|
|
|
|
import { EdgeGroup } from '@/react/edge/edge-groups/types';
|
2024-04-08 14:22:43 +00:00
|
|
|
import { RegistryId } from '@/react/portainer/registries/types/registry';
|
2023-11-15 12:43:18 +00:00
|
|
|
import { Pair } from '@/react/portainer/settings/types';
|
|
|
|
import {
|
2024-06-26 20:48:03 +00:00
|
|
|
AutoUpdateResponse,
|
2023-11-15 12:43:18 +00:00
|
|
|
GitFormModel,
|
|
|
|
RelativePathModel,
|
|
|
|
} from '@/react/portainer/gitops/types';
|
2024-05-06 05:08:03 +00:00
|
|
|
import { saveGitCredentialsIfNeeded } from '@/react/portainer/account/git-credentials/queries/useCreateGitCredentialsMutation';
|
|
|
|
import { UserId } from '@/portainer/users/types';
|
2023-11-15 12:43:18 +00:00
|
|
|
|
|
|
|
import { DeploymentType, StaggerConfig } from '../../types';
|
|
|
|
|
|
|
|
import { createStackFromFile } from './createStackFromFile';
|
|
|
|
import { createStackFromFileContent } from './createStackFromFileContent';
|
|
|
|
import { createStackFromGit } from './createStackFromGit';
|
|
|
|
|
|
|
|
export function useCreateEdgeStack() {
|
|
|
|
return useMutation(createEdgeStack);
|
|
|
|
}
|
|
|
|
|
2024-05-06 05:08:03 +00:00
|
|
|
export type BasePayload = {
|
|
|
|
userId: UserId;
|
2023-11-15 12:43:18 +00:00
|
|
|
/** Name of the stack */
|
|
|
|
name: string;
|
|
|
|
/** Content of the Stack file */
|
|
|
|
/** List of identifiers of EdgeGroups */
|
|
|
|
edgeGroups: Array<EdgeGroup['Id']>;
|
|
|
|
/** Deployment type to deploy this stack */
|
|
|
|
deploymentType: DeploymentType;
|
|
|
|
/** List of Registries to use for this stack */
|
|
|
|
registries?: Array<RegistryId>;
|
|
|
|
/** Uses the manifest's namespaces instead of the default one */
|
|
|
|
useManifestNamespaces?: boolean;
|
|
|
|
/** Pre Pull image */
|
|
|
|
prePullImage?: boolean;
|
|
|
|
/** Retry deploy */
|
|
|
|
retryDeploy?: boolean;
|
|
|
|
/** List of environment variables */
|
|
|
|
envVars?: Array<Pair>;
|
|
|
|
/** Configuration for stagger updates */
|
|
|
|
staggerConfig?: StaggerConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Payload for creating an EdgeStack from a string
|
|
|
|
*/
|
|
|
|
export type CreateEdgeStackPayload =
|
|
|
|
| {
|
|
|
|
method: 'file';
|
|
|
|
payload: BasePayload & {
|
|
|
|
/** File to upload */
|
|
|
|
file: File;
|
|
|
|
/** Optional webhook configuration */
|
|
|
|
webhook?: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
method: 'string';
|
|
|
|
payload: BasePayload & {
|
|
|
|
/** Content of the Stack file */
|
|
|
|
fileContent: string;
|
|
|
|
/** Optional webhook configuration */
|
|
|
|
webhook?: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
method: 'git';
|
|
|
|
payload: BasePayload & {
|
|
|
|
git: GitFormModel;
|
|
|
|
relativePathSettings?: RelativePathModel;
|
2024-06-26 20:48:03 +00:00
|
|
|
autoUpdate: AutoUpdateResponse | null;
|
2023-11-15 12:43:18 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
function createEdgeStack({ method, payload }: CreateEdgeStackPayload) {
|
|
|
|
switch (method) {
|
|
|
|
case 'file':
|
|
|
|
return createStackFromFile({
|
|
|
|
DeploymentType: payload.deploymentType,
|
|
|
|
EdgeGroups: payload.edgeGroups,
|
|
|
|
Name: payload.name,
|
|
|
|
file: payload.file,
|
|
|
|
EnvVars: payload.envVars,
|
|
|
|
PrePullImage: payload.prePullImage,
|
|
|
|
Registries: payload.registries,
|
|
|
|
RetryDeploy: payload.retryDeploy,
|
|
|
|
StaggerConfig: payload.staggerConfig,
|
|
|
|
UseManifestNamespaces: payload.useManifestNamespaces,
|
|
|
|
Webhook: payload.webhook,
|
|
|
|
});
|
|
|
|
case 'git':
|
2024-05-06 05:08:03 +00:00
|
|
|
return createStackAndGitCredential(payload.userId, payload);
|
2023-11-15 12:43:18 +00:00
|
|
|
case 'string':
|
|
|
|
return createStackFromFileContent({
|
|
|
|
deploymentType: payload.deploymentType,
|
|
|
|
edgeGroups: payload.edgeGroups,
|
|
|
|
name: payload.name,
|
|
|
|
envVars: payload.envVars,
|
|
|
|
prePullImage: payload.prePullImage,
|
|
|
|
registries: payload.registries,
|
|
|
|
retryDeploy: payload.retryDeploy,
|
|
|
|
staggerConfig: payload.staggerConfig,
|
|
|
|
useManifestNamespaces: payload.useManifestNamespaces,
|
|
|
|
stackFileContent: payload.fileContent,
|
|
|
|
webhook: payload.webhook,
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
throw new Error('Invalid method');
|
|
|
|
}
|
|
|
|
}
|
2024-05-06 05:08:03 +00:00
|
|
|
|
|
|
|
async function createStackAndGitCredential(
|
|
|
|
userId: UserId,
|
|
|
|
payload: BasePayload & {
|
|
|
|
git: GitFormModel;
|
|
|
|
relativePathSettings?: RelativePathModel;
|
2024-06-26 20:48:03 +00:00
|
|
|
autoUpdate: AutoUpdateResponse | null;
|
2024-05-06 05:08:03 +00:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
const newGitModel = await saveGitCredentialsIfNeeded(userId, payload.git);
|
|
|
|
|
|
|
|
return createStackFromGit({
|
|
|
|
deploymentType: payload.deploymentType,
|
|
|
|
edgeGroups: payload.edgeGroups,
|
|
|
|
name: payload.name,
|
|
|
|
envVars: payload.envVars,
|
|
|
|
prePullImage: payload.prePullImage,
|
|
|
|
registries: payload.registries,
|
|
|
|
retryDeploy: payload.retryDeploy,
|
|
|
|
staggerConfig: payload.staggerConfig,
|
|
|
|
useManifestNamespaces: payload.useManifestNamespaces,
|
|
|
|
repositoryUrl: newGitModel.RepositoryURL,
|
|
|
|
repositoryReferenceName: newGitModel.RepositoryReferenceName,
|
|
|
|
filePathInRepository: newGitModel.ComposeFilePathInRepository,
|
|
|
|
repositoryAuthentication: newGitModel.RepositoryAuthentication,
|
|
|
|
repositoryUsername: newGitModel.RepositoryUsername,
|
|
|
|
repositoryPassword: newGitModel.RepositoryPassword,
|
|
|
|
repositoryGitCredentialId: newGitModel.RepositoryGitCredentialID,
|
|
|
|
filesystemPath: payload.relativePathSettings?.FilesystemPath,
|
|
|
|
supportRelativePath: payload.relativePathSettings?.SupportRelativePath,
|
|
|
|
perDeviceConfigsGroupMatchType:
|
|
|
|
payload.relativePathSettings?.PerDeviceConfigsGroupMatchType,
|
|
|
|
perDeviceConfigsMatchType:
|
|
|
|
payload.relativePathSettings?.PerDeviceConfigsMatchType,
|
|
|
|
perDeviceConfigsPath: payload.relativePathSettings?.PerDeviceConfigsPath,
|
|
|
|
tlsSkipVerify: newGitModel.TLSSkipVerify,
|
2024-06-26 20:48:03 +00:00
|
|
|
autoUpdate: payload.autoUpdate,
|
2024-05-06 05:08:03 +00:00
|
|
|
});
|
|
|
|
}
|