From a0af919892ddd4cafe664e745750210ce0c624a4 Mon Sep 17 00:00:00 2001 From: hookenz Date: Thu, 11 Jan 2024 18:28:46 +1300 Subject: [PATCH] use docker run to deploy stack for agent upgrade --- edge/stack/stack.go | 8 ++++++++ exec/docker_compose_stack.go | 16 ++++++++++++++++ go.mod | 4 ++-- go.sum | 8 ++++---- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/edge/stack/stack.go b/edge/stack/stack.go index 735dee1f..9ec80c2c 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 deaf3377..e8a3df9d 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 d2fb7f9c..a032eadb 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.20231227185441-27e997fe0d1e + github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa github.com/rs/zerolog v1.29.0 github.com/wI2L/jsondiff v0.2.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 @@ -116,7 +116,7 @@ require ( golang.org/x/sys v0.15.0 // indirect golang.org/x/term v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect - golang.org/x/time v0.1.0 // indirect + golang.org/x/time v0.5.0 // indirect golang.org/x/tools v0.16.1 // indirect google.golang.org/appengine v1.6.8 // indirect google.golang.org/protobuf v1.32.0 // indirect diff --git a/go.sum b/go.sum index 5e66315a..82442582 100644 --- a/go.sum +++ b/go.sum @@ -277,8 +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.20231227185441-27e997fe0d1e h1:XWtJUsjPDJ8F2SsYBrPT+M9bleZG3wZx652jzCEB+zo= -github.com/portainer/portainer v0.6.1-0.20231227185441-27e997fe0d1e/go.mod h1:73Zf/oSGvxq8/u0AdmXGWwyiUytLz3LbUygJF3KjgmM= +github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa h1:1Khny+FwWWEkY8DYDBeHDCrRuoWlMMxtoQIZ52R22xM= +github.com/portainer/portainer v0.6.1-0.20240109020621-68dddbabbfaa/go.mod h1:L+FcDxUE5tztHmIZd8rCeuPwI15IOPgj7RXRSXTDiks= 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= @@ -419,8 +419,8 @@ golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA= -golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=