From 240b826af5b170e6dd665fcef4017110ae7d7380 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Thu, 18 Jan 2024 02:04:00 +0400 Subject: [PATCH] refactor(stack): deploy tasks --- .gitignore | 2 +- agent.go | 3 +++ edge/stack/stack.go | 14 ++++++-------- exec/docker_compose_stack.go | 13 +++++++------ go.mod | 2 ++ go.sum | 2 -- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 5fc404e57..1eac33901 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -dist +dist* .tmp .idea /.vscode/ diff --git a/agent.go b/agent.go index 40afe7ef5..8f564bb91 100644 --- a/agent.go +++ b/agent.go @@ -211,6 +211,9 @@ type ( DeployOptions struct { DeployerBaseOptions Prune bool + // Task is the name of the short lived task to deploy, if non empty will run the service as a task + // (docker compose will be run with `run --rm` instead of `up`) + Task string } RemoveOptions struct { diff --git a/edge/stack/stack.go b/edge/stack/stack.go index 15a47922d..3ecfe11bb 100644 --- a/edge/stack/stack.go +++ b/edge/stack/stack.go @@ -358,14 +358,6 @@ func (manager *StackManager) performActionOnStack(queueSleepInterval time.Durati } } - agentUpgrade := stack.EdgeUpdateID != 0 - if agentUpgrade { - stack.EnvVars = append(stack.EnvVars, portainer.Pair{ - Name: "_agentUpgrade", - Value: "true", - }) - } - manager.deployStack(ctx, stack, stackName, stackFileLocation) case actionDelete: stackFileLocation = fmt.Sprintf("%s/%s", SuccessStackFileFolder(stack.FileFolder), stack.FileName) @@ -575,6 +567,11 @@ func (manager *StackManager) deployStack(ctx context.Context, stack *edgeStack, envVars := buildEnvVarsForDeployer(stack.EnvVars) + task := "" + if stack.EdgeUpdateID != 0 { + task = "updater" + } + err = manager.deployer.Deploy(ctx, stackName, []string{stackFileLocation}, agent.DeployOptions{ DeployerBaseOptions: agent.DeployerBaseOptions{ @@ -582,6 +579,7 @@ func (manager *StackManager) deployStack(ctx context.Context, stack *edgeStack, WorkingDir: stack.FileFolder, Env: envVars, }, + Task: task, }, ) diff --git a/exec/docker_compose_stack.go b/exec/docker_compose_stack.go index e8a3df9d7..899a4a65c 100644 --- a/exec/docker_compose_stack.go +++ b/exec/docker_compose_stack.go @@ -2,7 +2,6 @@ package exec import ( "context" - "slices" "github.com/portainer/agent" libstack "github.com/portainer/portainer/pkg/libstack" @@ -33,16 +32,18 @@ func NewDockerComposeStackService(binaryPath string) (*DockerComposeStackService // Deploy executes the docker stack deploy command. func (service *DockerComposeStackService) Deploy(ctx context.Context, name string, filePaths []string, options agent.DeployOptions) error { - // TODO: this should probably be implemented as an edge job in future. - agentUpgrade := slices.Contains(options.Env, "_agentUpgrade") - if agentUpgrade { - log.Debug().Msgf("Detected portainer agent upgrade") - return service.deployer.Run(ctx, filePaths, "updater", libstack.RunOptions{ + if options.Task != "" { + log.Debug(). + Str("task name", options.Task). + Msg("Detected short lived task run") + + return service.deployer.Run(ctx, filePaths, options.Task, libstack.RunOptions{ Options: libstack.Options{ ProjectName: name, WorkingDir: options.WorkingDir, Env: options.Env, }, + Remove: true, }) } diff --git a/go.mod b/go.mod index f68965aa8..e8955153c 100644 --- a/go.mod +++ b/go.mod @@ -133,3 +133,5 @@ require ( ) replace github.com/jaguilar/vt100 => github.com/tonistiigi/vt100 v0.0.0-20190402012908-ad4c4a574305 + +replace github.com/portainer/portainer => ../portainer diff --git a/go.sum b/go.sum index 80677e35c..16e1c19b6 100644 --- a/go.sum +++ b/go.sum @@ -277,8 +277,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/portainer/portainer v0.6.1-0.20240117161906-171b42d11821 h1:HfwxvbVbPgeme4CLKNUwqfb6OwZyHC1hWoKCBCILRio= -github.com/portainer/portainer v0.6.1-0.20240117161906-171b42d11821/go.mod h1:4sPWxbrwbD8T+W4EEwRWPTq8goN8n3ntlQm6AmZC+XA= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=