From f93c807f3bff9b94c809593ef5f96884d2b54ed2 Mon Sep 17 00:00:00 2001 From: hookenz Date: Thu, 11 Jan 2024 18:28:46 +1300 Subject: [PATCH 1/3] use docker run to deploy stack for agent upgrade --- edge/stack/stack.go | 8 ++++++++ exec/docker_compose_stack.go | 16 ++++++++++++++++ go.mod | 2 +- go.sum | 8 ++------ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/edge/stack/stack.go b/edge/stack/stack.go index a55be13f0..15a47922d 100644 --- a/edge/stack/stack.go +++ b/edge/stack/stack.go @@ -358,6 +358,14 @@ 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) diff --git a/exec/docker_compose_stack.go b/exec/docker_compose_stack.go index deaf33770..e8a3df9d7 100644 --- a/exec/docker_compose_stack.go +++ b/exec/docker_compose_stack.go @@ -2,10 +2,12 @@ package exec import ( "context" + "slices" "github.com/portainer/agent" libstack "github.com/portainer/portainer/pkg/libstack" "github.com/portainer/portainer/pkg/libstack/compose" + "github.com/rs/zerolog/log" ) // DockerComposeStackService represents a service for managing stacks by using the Docker binary. @@ -30,6 +32,20 @@ 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{ + Options: libstack.Options{ + ProjectName: name, + WorkingDir: options.WorkingDir, + Env: options.Env, + }, + }) + } + return service.deployer.Deploy(ctx, filePaths, libstack.DeployOptions{ Options: libstack.Options{ ProjectName: name, diff --git a/go.mod b/go.mod index bcf26989c..f68965aa8 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/opencontainers/image-spec v1.1.0-rc5 github.com/pkg/errors v0.9.1 - github.com/portainer/portainer v0.6.1-0.20240116212059-47f29002f0dc + github.com/portainer/portainer v0.6.1-0.20240117161906-171b42d11821 github.com/rs/zerolog v1.29.0 github.com/wI2L/jsondiff v0.2.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 diff --git a/go.sum b/go.sum index 63c6fb210..80677e35c 100644 --- a/go.sum +++ b/go.sum @@ -277,12 +277,8 @@ 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.20240112073152-f9c0a1c26b72 h1:3grxIEsc2GPBXZga6tA5P0dTjHGFUiE5rKvnV6+kB3I= -github.com/portainer/portainer v0.6.1-0.20240112073152-f9c0a1c26b72/go.mod h1:4sPWxbrwbD8T+W4EEwRWPTq8goN8n3ntlQm6AmZC+XA= -github.com/portainer/portainer v0.6.1-0.20240115043120-17c6eb6d423a h1:AUNvx2+hfvm5TEk1UBiK4zfqM7x5lyRNh1nWKUE8nn4= -github.com/portainer/portainer v0.6.1-0.20240115043120-17c6eb6d423a/go.mod h1:4sPWxbrwbD8T+W4EEwRWPTq8goN8n3ntlQm6AmZC+XA= -github.com/portainer/portainer v0.6.1-0.20240116212059-47f29002f0dc h1:6E+0WPzNvCVLdtvqHvrIQEYI2CwLxhgzusu8TKXji6Q= -github.com/portainer/portainer v0.6.1-0.20240116212059-47f29002f0dc/go.mod h1:4sPWxbrwbD8T+W4EEwRWPTq8goN8n3ntlQm6AmZC+XA= +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= From 240b826af5b170e6dd665fcef4017110ae7d7380 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Thu, 18 Jan 2024 02:04:00 +0400 Subject: [PATCH 2/3] 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= From 7d83b65e9f0d72eef79fe43faaf3f2d17f88ca7a Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Tue, 23 Jan 2024 11:27:04 +0200 Subject: [PATCH 3/3] chore(deps): upgrade portainer --- go.mod | 4 +--- go.sum | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e8955153c..a9f16de68 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/mitchellh/mapstructure v1.5.0 github.com/opencontainers/image-spec v1.1.0-rc5 github.com/pkg/errors v0.9.1 - github.com/portainer/portainer v0.6.1-0.20240117161906-171b42d11821 + github.com/portainer/portainer v0.6.1-0.20240123081012-ff330ccf4005 github.com/rs/zerolog v1.29.0 github.com/wI2L/jsondiff v0.2.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 @@ -133,5 +133,3 @@ 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 16e1c19b6..aaa512e64 100644 --- a/go.sum +++ b/go.sum @@ -277,6 +277,8 @@ 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.20240123081012-ff330ccf4005 h1:PyeHnran8CY88LRCpOpNP/GZWJzTOu7brz7Cf3pblOo= +github.com/portainer/portainer v0.6.1-0.20240123081012-ff330ccf4005/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=