2022-04-14 10:14:23 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import { getAgentShortVersion } from '@/portainer/views/endpoints/helpers';
|
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
import { ScriptFormValues, Platform } from './types';
|
2022-04-14 10:14:23 +00:00
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
type CommandGenerator = (
|
|
|
|
agentVersion: string,
|
|
|
|
edgeKey: string,
|
|
|
|
properties: ScriptFormValues,
|
2022-11-21 07:51:55 +00:00
|
|
|
useAsyncMode: boolean,
|
2022-06-01 04:28:31 +00:00
|
|
|
edgeId?: string,
|
|
|
|
agentSecret?: string
|
|
|
|
) => string;
|
2022-04-14 10:14:23 +00:00
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
export type CommandTab = {
|
|
|
|
id: Platform;
|
|
|
|
label: string;
|
|
|
|
command: CommandGenerator;
|
|
|
|
};
|
2022-04-14 10:14:23 +00:00
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
export const commandsTabs: Record<string, CommandTab> = {
|
|
|
|
k8sLinux: {
|
|
|
|
id: 'k8s',
|
|
|
|
label: 'Kubernetes',
|
|
|
|
command: buildLinuxKubernetesCommand,
|
|
|
|
},
|
|
|
|
swarmLinux: {
|
|
|
|
id: 'swarm',
|
|
|
|
label: 'Docker Swarm',
|
|
|
|
command: buildLinuxSwarmCommand,
|
|
|
|
},
|
|
|
|
standaloneLinux: {
|
|
|
|
id: 'standalone',
|
|
|
|
label: 'Docker Standalone',
|
|
|
|
command: buildLinuxStandaloneCommand,
|
|
|
|
},
|
|
|
|
swarmWindows: {
|
|
|
|
id: 'swarm',
|
|
|
|
label: 'Docker Swarm',
|
|
|
|
command: buildWindowsSwarmCommand,
|
|
|
|
},
|
|
|
|
standaloneWindow: {
|
|
|
|
id: 'standalone',
|
|
|
|
label: 'Docker Standalone',
|
|
|
|
command: buildWindowsStandaloneCommand,
|
|
|
|
},
|
|
|
|
} as const;
|
2022-04-14 10:14:23 +00:00
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
export function buildLinuxStandaloneCommand(
|
2022-04-14 10:14:23 +00:00
|
|
|
agentVersion: string,
|
|
|
|
edgeKey: string,
|
2022-06-01 04:28:31 +00:00
|
|
|
properties: ScriptFormValues,
|
2022-11-21 07:51:55 +00:00
|
|
|
useAsyncMode: boolean,
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeId?: string,
|
|
|
|
agentSecret?: string
|
|
|
|
) {
|
2022-06-01 04:28:31 +00:00
|
|
|
const { allowSelfSignedCertificates, edgeIdGenerator, envVars } = properties;
|
|
|
|
|
2023-03-06 20:25:04 +00:00
|
|
|
const env = buildDockerEnvVars(envVars, [
|
|
|
|
...buildDefaultDockerEnvVars(
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeKey,
|
2022-06-01 04:28:31 +00:00
|
|
|
allowSelfSignedCertificates,
|
|
|
|
!edgeIdGenerator ? edgeId : undefined,
|
2022-11-21 07:51:55 +00:00
|
|
|
agentSecret,
|
|
|
|
useAsyncMode
|
2023-03-06 20:25:04 +00:00
|
|
|
),
|
|
|
|
...metaEnvVars(properties),
|
|
|
|
]);
|
2022-04-14 10:14:23 +00:00
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
return `${
|
|
|
|
edgeIdGenerator ? `PORTAINER_EDGE_ID=$(${edgeIdGenerator}) \n\n` : ''
|
|
|
|
}\
|
2022-04-14 10:14:23 +00:00
|
|
|
docker run -d \\
|
|
|
|
-v /var/run/docker.sock:/var/run/docker.sock \\
|
|
|
|
-v /var/lib/docker/volumes:/var/lib/docker/volumes \\
|
|
|
|
-v /:/host \\
|
|
|
|
-v portainer_agent_data:/data \\
|
|
|
|
--restart always \\
|
|
|
|
${env} \\
|
|
|
|
--name portainer_edge_agent \\
|
|
|
|
portainer/agent:${agentVersion}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
export function buildWindowsStandaloneCommand(
|
2022-04-14 10:14:23 +00:00
|
|
|
agentVersion: string,
|
|
|
|
edgeKey: string,
|
2022-06-01 04:28:31 +00:00
|
|
|
properties: ScriptFormValues,
|
2022-11-21 07:51:55 +00:00
|
|
|
useAsyncMode: boolean,
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeId?: string,
|
|
|
|
agentSecret?: string
|
|
|
|
) {
|
2022-06-01 04:28:31 +00:00
|
|
|
const { allowSelfSignedCertificates, edgeIdGenerator, envVars } = properties;
|
|
|
|
|
2023-03-06 20:25:04 +00:00
|
|
|
const env = buildDockerEnvVars(envVars, [
|
|
|
|
...buildDefaultDockerEnvVars(
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeKey,
|
2022-06-01 04:28:31 +00:00
|
|
|
allowSelfSignedCertificates,
|
|
|
|
edgeIdGenerator ? '$Env:PORTAINER_EDGE_ID' : edgeId,
|
2022-11-21 07:51:55 +00:00
|
|
|
agentSecret,
|
|
|
|
useAsyncMode
|
2023-03-06 20:25:04 +00:00
|
|
|
),
|
|
|
|
...metaEnvVars(properties),
|
|
|
|
]);
|
2022-04-14 10:14:23 +00:00
|
|
|
|
|
|
|
return `${
|
2022-06-01 04:28:31 +00:00
|
|
|
edgeIdGenerator
|
|
|
|
? `$Env:PORTAINER_EDGE_ID = "@(${edgeIdGenerator})" \n\n`
|
|
|
|
: ''
|
2022-04-28 21:44:34 +00:00
|
|
|
}\
|
2022-04-14 10:14:23 +00:00
|
|
|
docker run -d \\
|
|
|
|
--mount type=npipe,src=\\\\.\\pipe\\docker_engine,dst=\\\\.\\pipe\\docker_engine \\
|
|
|
|
--mount type=bind,src=C:\\ProgramData\\docker\\volumes,dst=C:\\ProgramData\\docker\\volumes \\
|
|
|
|
--mount type=volume,src=portainer_agent_data,dst=C:\\data \\
|
|
|
|
--restart always \\
|
|
|
|
${env} \\
|
|
|
|
--name portainer_edge_agent \\
|
|
|
|
portainer/agent:${agentVersion}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
export function buildLinuxSwarmCommand(
|
2022-04-14 10:14:23 +00:00
|
|
|
agentVersion: string,
|
|
|
|
edgeKey: string,
|
2022-06-01 04:28:31 +00:00
|
|
|
properties: ScriptFormValues,
|
2022-11-21 07:51:55 +00:00
|
|
|
useAsyncMode: boolean,
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeId?: string,
|
|
|
|
agentSecret?: string
|
|
|
|
) {
|
2022-06-01 04:28:31 +00:00
|
|
|
const { allowSelfSignedCertificates, edgeIdGenerator, envVars } = properties;
|
|
|
|
|
2022-04-14 10:14:23 +00:00
|
|
|
const env = buildDockerEnvVars(envVars, [
|
2023-03-06 20:25:04 +00:00
|
|
|
...buildDefaultDockerEnvVars(
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeKey,
|
2022-06-01 04:28:31 +00:00
|
|
|
allowSelfSignedCertificates,
|
|
|
|
!edgeIdGenerator ? edgeId : undefined,
|
2022-11-21 07:51:55 +00:00
|
|
|
agentSecret,
|
|
|
|
useAsyncMode
|
2022-04-14 10:14:23 +00:00
|
|
|
),
|
|
|
|
'AGENT_CLUSTER_ADDR=tasks.portainer_edge_agent',
|
2023-03-06 20:25:04 +00:00
|
|
|
...metaEnvVars(properties),
|
2022-04-14 10:14:23 +00:00
|
|
|
]);
|
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
return `${
|
|
|
|
edgeIdGenerator ? `PORTAINER_EDGE_ID=$(${edgeIdGenerator}) \n\n` : ''
|
|
|
|
}\
|
2022-04-14 10:14:23 +00:00
|
|
|
docker network create \\
|
|
|
|
--driver overlay \\
|
|
|
|
portainer_agent_network;
|
2022-06-01 04:28:31 +00:00
|
|
|
|
2022-04-14 10:14:23 +00:00
|
|
|
docker service create \\
|
|
|
|
--name portainer_edge_agent \\
|
|
|
|
--network portainer_agent_network \\
|
|
|
|
${env} \\
|
|
|
|
--mode global \\
|
|
|
|
--constraint 'node.platform.os == linux' \\
|
|
|
|
--mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \\
|
|
|
|
--mount type=bind,src=//var/lib/docker/volumes,dst=/var/lib/docker/volumes \\
|
|
|
|
--mount type=bind,src=//,dst=/host \\
|
|
|
|
--mount type=volume,src=portainer_agent_data,dst=/data \\
|
|
|
|
portainer/agent:${agentVersion}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
export function buildWindowsSwarmCommand(
|
2022-04-14 10:14:23 +00:00
|
|
|
agentVersion: string,
|
|
|
|
edgeKey: string,
|
2022-06-01 04:28:31 +00:00
|
|
|
properties: ScriptFormValues,
|
2022-11-21 07:51:55 +00:00
|
|
|
useAsyncMode: boolean,
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeId?: string,
|
|
|
|
agentSecret?: string
|
|
|
|
) {
|
2022-06-01 04:28:31 +00:00
|
|
|
const { allowSelfSignedCertificates, edgeIdGenerator, envVars } = properties;
|
|
|
|
|
2022-04-14 10:14:23 +00:00
|
|
|
const env = buildDockerEnvVars(envVars, [
|
2023-03-06 20:25:04 +00:00
|
|
|
...buildDefaultDockerEnvVars(
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeKey,
|
2022-06-01 04:28:31 +00:00
|
|
|
allowSelfSignedCertificates,
|
|
|
|
edgeIdGenerator ? '$Env:PORTAINER_EDGE_ID' : edgeId,
|
2022-11-21 07:51:55 +00:00
|
|
|
agentSecret,
|
|
|
|
useAsyncMode
|
2022-04-14 10:14:23 +00:00
|
|
|
),
|
|
|
|
'AGENT_CLUSTER_ADDR=tasks.portainer_edge_agent',
|
2023-03-06 20:25:04 +00:00
|
|
|
...metaEnvVars(properties),
|
2022-04-14 10:14:23 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return `${
|
2022-06-01 04:28:31 +00:00
|
|
|
edgeIdGenerator
|
|
|
|
? `$Env:PORTAINER_EDGE_ID = "@(${edgeIdGenerator})" \n\n`
|
|
|
|
: ''
|
|
|
|
}\
|
2022-04-14 10:14:23 +00:00
|
|
|
docker network create \\
|
|
|
|
--driver overlay \\
|
|
|
|
portainer_agent_network;
|
|
|
|
|
|
|
|
docker service create \\
|
|
|
|
--name portainer_edge_agent \\
|
|
|
|
--network portainer_agent_network \\
|
|
|
|
${env} \\
|
|
|
|
--mode global \\
|
|
|
|
--constraint 'node.platform.os == windows' \\
|
|
|
|
--mount type=npipe,src=\\\\.\\pipe\\docker_engine,dst=\\\\.\\pipe\\docker_engine \\
|
|
|
|
--mount type=bind,src=C:\\ProgramData\\docker\\volumes,dst=C:\\ProgramData\\docker\\volumes \\
|
|
|
|
--mount type=volume,src=portainer_agent_data,dst=C:\\data \\
|
|
|
|
portainer/agent:${agentVersion}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
export function buildLinuxKubernetesCommand(
|
2022-04-14 10:14:23 +00:00
|
|
|
agentVersion: string,
|
|
|
|
edgeKey: string,
|
2022-06-01 04:28:31 +00:00
|
|
|
properties: ScriptFormValues,
|
2022-11-21 07:51:55 +00:00
|
|
|
useAsyncMode: boolean,
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeId?: string,
|
2022-06-01 04:28:31 +00:00
|
|
|
agentSecret?: string
|
2022-04-14 10:14:23 +00:00
|
|
|
) {
|
2022-06-01 04:28:31 +00:00
|
|
|
const { allowSelfSignedCertificates, edgeIdGenerator, envVars } = properties;
|
|
|
|
|
2022-04-14 10:14:23 +00:00
|
|
|
const agentShortVersion = getAgentShortVersion(agentVersion);
|
2023-03-06 20:25:04 +00:00
|
|
|
const allEnvVars = buildEnvVars(
|
|
|
|
envVars,
|
|
|
|
_.compact([useAsyncMode && 'EDGE_ASYNC=1', ...metaEnvVars(properties)])
|
|
|
|
);
|
|
|
|
|
2022-06-01 04:28:31 +00:00
|
|
|
const idEnvVar = edgeIdGenerator
|
|
|
|
? `PORTAINER_EDGE_ID=$(${edgeIdGenerator}) \n\n`
|
|
|
|
: '';
|
|
|
|
const edgeIdVar = !edgeIdGenerator && edgeId ? edgeId : '$PORTAINER_EDGE_ID';
|
|
|
|
const selfSigned = allowSelfSignedCertificates ? '1' : '0';
|
2022-04-14 10:14:23 +00:00
|
|
|
|
2023-03-06 20:25:04 +00:00
|
|
|
return `${idEnvVar}curl https://downloads.portainer.io/ee${agentShortVersion}/portainer-edge-agent-setup.sh | bash -s -- "${edgeIdVar}" "${edgeKey}" "${selfSigned}" "${agentSecret}" "${allEnvVars}"`;
|
2022-04-14 10:14:23 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 20:25:04 +00:00
|
|
|
function buildDockerEnvVars(envVars: string, moreVars: string[]) {
|
|
|
|
const vars = moreVars.concat(envVars.split(',').filter((s) => s.length > 0));
|
|
|
|
|
|
|
|
return vars.map((s) => `-e ${s}`).join(' \\\n ');
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildDefaultDockerEnvVars(
|
2022-04-14 10:14:23 +00:00
|
|
|
edgeKey: string,
|
|
|
|
allowSelfSignedCerts: boolean,
|
|
|
|
edgeId = '$PORTAINER_EDGE_ID',
|
2022-11-21 07:51:55 +00:00
|
|
|
agentSecret = '',
|
|
|
|
useAsyncMode = false
|
2022-04-14 10:14:23 +00:00
|
|
|
) {
|
|
|
|
return _.compact([
|
|
|
|
'EDGE=1',
|
|
|
|
`EDGE_ID=${edgeId}`,
|
|
|
|
`EDGE_KEY=${edgeKey}`,
|
|
|
|
`EDGE_INSECURE_POLL=${allowSelfSignedCerts ? 1 : 0}`,
|
|
|
|
agentSecret ? `AGENT_SECRET=${agentSecret}` : ``,
|
2022-11-21 07:51:55 +00:00
|
|
|
useAsyncMode ? 'EDGE_ASYNC=1' : '',
|
2022-04-14 10:14:23 +00:00
|
|
|
]);
|
|
|
|
}
|
2023-03-06 20:25:04 +00:00
|
|
|
|
|
|
|
const ENV_VAR_SEPARATOR = ',';
|
|
|
|
const VAR_LIST_SEPARATOR = ':';
|
|
|
|
function buildEnvVars(envVars: string, moreVars: string[]) {
|
|
|
|
return _.compact([envVars.trim(), ...moreVars]).join(ENV_VAR_SEPARATOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
function metaEnvVars({
|
|
|
|
edgeGroupsIds,
|
|
|
|
group,
|
|
|
|
tagsIds,
|
|
|
|
}: Pick<ScriptFormValues, 'edgeGroupsIds' | 'tagsIds' | 'group'>) {
|
|
|
|
return _.compact([
|
|
|
|
edgeGroupsIds.length &&
|
|
|
|
`EDGE_GROUPS=${edgeGroupsIds.join(VAR_LIST_SEPARATOR)}`,
|
|
|
|
group && `PORTAINER_GROUP=${group}`,
|
|
|
|
tagsIds.length && `PORTAINER_TAGS=${tagsIds.join(VAR_LIST_SEPARATOR)}`,
|
|
|
|
]);
|
|
|
|
}
|