From 0cb5656db6f021781cdbddb2559e9e7dc9b860be Mon Sep 17 00:00:00 2001 From: cong meng Date: Thu, 30 Sep 2021 21:07:13 +1300 Subject: [PATCH] feat(frontend) auto generate agent version EE-1266 (#5794) Co-authored-by: Simon Meng --- .../create/createEndpointController.js | 15 +++++--- .../views/endpoints/edit/endpoint.html | 6 ++-- .../endpoints/edit/endpointController.js | 35 ++++++++++++------- app/portainer/views/endpoints/helpers.js | 4 +++ .../wizard-docker.controller.js | 8 +++-- .../wizard-kubernetes.controller.js | 8 +++-- 6 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 app/portainer/views/endpoints/helpers.js diff --git a/app/portainer/views/endpoints/create/createEndpointController.js b/app/portainer/views/endpoints/create/createEndpointController.js index 828ecbae9..69720fee1 100644 --- a/app/portainer/views/endpoints/create/createEndpointController.js +++ b/app/portainer/views/endpoints/create/createEndpointController.js @@ -1,4 +1,5 @@ import { PortainerEndpointCreationTypes, PortainerEndpointTypes } from 'Portainer/models/endpoint/models'; +import { getAgentShortVersion } from 'Portainer/views/endpoints/helpers'; import { EndpointSecurityFormData } from '../../../components/endpointSecurity/porEndpointSecurityModel'; angular @@ -16,7 +17,8 @@ angular TagService, SettingsService, Notifications, - Authentication + Authentication, + StateManager ) { $scope.state = { EnvironmentType: 'agent', @@ -44,11 +46,14 @@ angular ], }; + const agentVersion = StateManager.getState().application.version; + const agentShortVersion = getAgentShortVersion(agentVersion); + const deployCommands = { - kubeLoadBalancer: `curl -L https://downloads.portainer.io/portainer-agent-k8s-lb.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml`, - kubeNodePort: `curl -L https://downloads.portainer.io/portainer-agent-k8s-nodeport.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml`, - agentLinux: `curl -L https://downloads.portainer.io/agent-stack.yml -o agent-stack.yml && docker stack deploy --compose-file=agent-stack.yml portainer-agent`, - agentWindows: `curl -L https://downloads.portainer.io/agent-stack-windows.yml -o agent-stack-windows.yml && docker stack deploy --compose-file=agent-stack-windows.yml portainer-agent`, + kubeLoadBalancer: `curl -L https://downloads.portainer.io/portainer-agent-ce${agentShortVersion}-k8s-lb.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml`, + kubeNodePort: `curl -L https://downloads.portainer.io/portainer-agent-ce${agentShortVersion}-k8s-nodeport.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml`, + agentLinux: `curl -L https://downloads.portainer.io/agent-stack-ce${agentShortVersion}.yml -o agent-stack.yml && docker stack deploy --compose-file=agent-stack.yml portainer-agent`, + agentWindows: `curl -L https://downloads.portainer.io/agent-stack-ce${agentShortVersion}-windows.yml -o agent-stack-windows.yml && docker stack deploy --compose-file=agent-stack-windows.yml portainer-agent`, }; $scope.deployCommands = deployCommands; diff --git a/app/portainer/views/endpoints/edit/endpoint.html b/app/portainer/views/endpoints/edit/endpoint.html index aeb80bd9b..c76c1ef83 100644 --- a/app/portainer/views/endpoints/edit/endpoint.html +++ b/app/portainer/views/endpoints/edit/endpoint.html @@ -62,17 +62,17 @@ - {{ dockerCommands[state.deploymentTab][state.platformType](randomEdgeID, endpoint.EdgeKey, state.allowSelfSignedCerts) }} + {{ dockerCommands[state.deploymentTab][state.platformType](agentVersion, agentShortVersion, randomEdgeID, endpoint.EdgeKey, state.allowSelfSignedCerts) }} - {{ dockerCommands[state.deploymentTab][state.platformType](randomEdgeID, endpoint.EdgeKey, state.allowSelfSignedCerts) }} + {{ dockerCommands[state.deploymentTab][state.platformType](agentVersion, agentShortVersion, randomEdgeID, endpoint.EdgeKey, state.allowSelfSignedCerts) }} - {{ dockerCommands[state.deploymentTab][state.platformType](randomEdgeID, endpoint.EdgeKey, state.allowSelfSignedCerts) }} + {{ dockerCommands[state.deploymentTab][state.platformType](agentVersion, agentShortVersion, randomEdgeID, endpoint.EdgeKey, state.allowSelfSignedCerts) }} diff --git a/app/portainer/views/endpoints/edit/endpointController.js b/app/portainer/views/endpoints/edit/endpointController.js index 0e173188c..6163d5d71 100644 --- a/app/portainer/views/endpoints/edit/endpointController.js +++ b/app/portainer/views/endpoints/edit/endpointController.js @@ -3,6 +3,7 @@ import uuidv4 from 'uuid/v4'; import { PortainerEndpointTypes } from '@/portainer/models/endpoint/models'; import { EndpointSecurityFormData } from '@/portainer/components/endpointSecurity/porEndpointSecurityModel'; +import { getAgentShortVersion } from 'Portainer/views/endpoints/helpers'; angular.module('portainer.app').controller('EndpointController', EndpointController); @@ -22,7 +23,8 @@ function EndpointController( Notifications, Authentication, SettingsService, - ModalService + ModalService, + StateManager ) { const DEPLOYMENT_TABS = { SWARM: 'swarm', @@ -66,6 +68,9 @@ function EndpointController( allowSelfSignedCerts: true, }; + $scope.agentVersion = StateManager.getState().application.version; + $scope.agentShortVersion = getAgentShortVersion($scope.agentVersion); + $scope.dockerCommands = { [DEPLOYMENT_TABS.STANDALONE]: { [PLATFORM_TYPES.LINUX]: buildLinuxStandaloneCommand, @@ -87,7 +92,13 @@ function EndpointController( $scope.copyEdgeAgentDeploymentCommand = copyEdgeAgentDeploymentCommand; function copyEdgeAgentDeploymentCommand() { - const command = $scope.dockerCommands[$scope.state.deploymentTab][$scope.state.platformType]($scope.randomEdgeID, $scope.endpoint.EdgeKey, $scope.state.allowSelfSignedCerts); + const command = $scope.dockerCommands[$scope.state.deploymentTab][$scope.state.platformType]( + $scope.agentVersion, + $scope.agentShortVersion, + $scope.randomEdgeID, + $scope.endpoint.EdgeKey, + $scope.state.allowSelfSignedCerts + ); clipboard.copyText(command.trim()); $('#copyNotificationDeploymentCommand').show().fadeOut(2500); } @@ -269,7 +280,7 @@ function EndpointController( }); } - function buildLinuxStandaloneCommand(edgeId, edgeKey, allowSelfSignedCerts) { + function buildLinuxStandaloneCommand(agentVersion, agentShortVersion, edgeId, edgeKey, allowSelfSignedCerts) { return ` docker run -d \\ -v /var/run/docker.sock:/var/run/docker.sock \\ @@ -283,10 +294,10 @@ docker run -d \\ -e CAP_HOST_MANAGEMENT=1 \\ -e EDGE_INSECURE_POLL=${allowSelfSignedCerts ? 1 : 0} \\ --name portainer_edge_agent \\ - portainer/agent`; + portainer/agent:${agentVersion}`; } - function buildWindowsStandaloneCommand(edgeId, edgeKey, allowSelfSignedCerts) { + function buildWindowsStandaloneCommand(agentVersion, agentShortVersion, edgeId, edgeKey, allowSelfSignedCerts) { return ` docker run -d \\ --mount type=npipe,src=\\\\.\\pipe\\docker_engine,dst=\\\\.\\pipe\\docker_engine \\ @@ -299,10 +310,10 @@ docker run -d \\ -e CAP_HOST_MANAGEMENT=1 \\ -e EDGE_INSECURE_POLL=${allowSelfSignedCerts ? 1 : 0} \\ --name portainer_edge_agent \\ - portainer/agent`; + portainer/agent:${agentVersion}`; } - function buildLinuxSwarmCommand(edgeId, edgeKey, allowSelfSignedCerts) { + function buildLinuxSwarmCommand(agentVersion, agentShortVersion, edgeId, edgeKey, allowSelfSignedCerts) { return ` docker network create \\ --driver overlay \\ @@ -323,10 +334,10 @@ docker service create \\ --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`; + portainer/agent:${agentVersion}`; } - function buildWindowsSwarmCommand(edgeId, edgeKey, allowSelfSignedCerts) { + function buildWindowsSwarmCommand(agentVersion, agentShortVersion, edgeId, edgeKey, allowSelfSignedCerts) { return ` docker network create \\ --driver overlay \\ @@ -345,12 +356,12 @@ docker service create \\ --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`; + portainer/agent:${agentVersion}`; } - function buildKubernetesCommand(edgeId, edgeKey, allowSelfSignedCerts) { + function buildKubernetesCommand(agentVersion, agentShortVersion, edgeId, edgeKey, allowSelfSignedCerts) { return ` -curl https://downloads.portainer.io/portainer-ce29-edge-agent-setup.sh | bash -s -- ${edgeId} ${edgeKey} ${allowSelfSignedCerts ? '1' : ''} +curl https://downloads.portainer.io/portainer-ce${agentShortVersion}-edge-agent-setup.sh | bash -s -- ${edgeId} ${edgeKey} ${allowSelfSignedCerts ? '1' : ''} `; } diff --git a/app/portainer/views/endpoints/helpers.js b/app/portainer/views/endpoints/helpers.js new file mode 100644 index 000000000..6aaaff31e --- /dev/null +++ b/app/portainer/views/endpoints/helpers.js @@ -0,0 +1,4 @@ +export function getAgentShortVersion(agentVersion) { + const numbers = agentVersion.split('.'); + return numbers[0] + numbers[1]; +} diff --git a/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-docker/wizard-docker.controller.js b/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-docker/wizard-docker.controller.js index 5d82d6197..f11002b3b 100644 --- a/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-docker/wizard-docker.controller.js +++ b/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-docker/wizard-docker.controller.js @@ -2,6 +2,7 @@ import { PortainerEndpointCreationTypes } from 'Portainer/models/endpoint/models'; import { buildOption } from '@/portainer/components/box-selector'; import { EndpointSecurityFormData } from 'Portainer/components/endpointSecurity/porEndpointSecurityModel'; +import { getAgentShortVersion } from 'Portainer/views/endpoints/helpers'; export default class WizardDockerController { /* @ngInject */ @@ -200,9 +201,12 @@ export default class WizardDockerController { securityFormData: new EndpointSecurityFormData(), }; + const agentVersion = this.StateManager.getState().application.version; + const agentShortVersion = getAgentShortVersion(agentVersion); + this.command = { - linuxCommand: `curl -L https://downloads.portainer.io/agent-stack.yml -o agent-stack.yml && docker stack deploy --compose-file=agent-stack.yml portainer-agent `, - winCommand: `curl -L https://downloads.portainer.io/agent-stack-windows.yml -o agent-stack-windows.yml && docker stack deploy --compose-file=agent-stack-windows.yml portainer-agent `, + linuxCommand: `curl -L https://downloads.portainer.io/agent-stack-ce${agentShortVersion}.yml -o agent-stack.yml && docker stack deploy --compose-file=agent-stack.yml portainer-agent `, + winCommand: `curl -L https://downloads.portainer.io/agent-stack-ce${agentShortVersion}-windows.yml -o agent-stack-windows.yml && docker stack deploy --compose-file=agent-stack-windows.yml portainer-agent `, linuxSocket: `-v "/var/run/docker.sock:/var/run/docker.sock" `, winSocket: `-v \.\pipe\docker_engine:\.\pipe\docker_engine `, }; diff --git a/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-kubernetes/wizard-kubernetes.controller.js b/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-kubernetes/wizard-kubernetes.controller.js index 0971e8b9b..f84f90e93 100644 --- a/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-kubernetes/wizard-kubernetes.controller.js +++ b/app/portainer/views/wizard/wizard-endpoints/wizard-endpoint-kubernetes/wizard-kubernetes.controller.js @@ -1,6 +1,7 @@ import { PortainerEndpointCreationTypes } from 'Portainer/models/endpoint/models'; //import { getAgentShortVersion } from 'Portainer/views/endpoints/helpers'; import { buildOption } from '@/portainer/components/box-selector'; +import { getAgentShortVersion } from 'Portainer/views/endpoints/helpers'; export default class WizardKubernetesController { /* @ngInject */ @@ -96,9 +97,12 @@ export default class WizardKubernetesController { availableOptions: [buildOption('Agent', 'fa fa-bolt', 'Agent', '', 'agent')], }; + const agentVersion = this.StateManager.getState().application.version; + const agentShortVersion = getAgentShortVersion(agentVersion); + this.command = { - loadBalancer: `curl -L https://downloads.portainer.io/portainer-agent-k8s-lb.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml `, - nodePort: `curl -L https://downloads.portainer.io/portainer-agent-k8s-nodeport.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml `, + loadBalancer: `curl -L https://downloads.portainer.io/portainer-agent-ce${agentShortVersion}-k8s-lb.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml `, + nodePort: `curl -L https://downloads.portainer.io/portainer-agent-ce${agentShortVersion}-k8s-nodeport.yaml -o portainer-agent-k8s.yaml; kubectl apply -f portainer-agent-k8s.yaml `, }; }); }