Skip to content

Commit

Permalink
refactor(stack): deploy tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaim Lev-Ari committed Jan 17, 2024
1 parent f93c807 commit 240b826
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist
dist*
.tmp
.idea
/.vscode/
Expand Down
3 changes: 3 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions edge/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -575,13 +567,19 @@ 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{
Namespace: stack.Namespace,
WorkingDir: stack.FileFolder,
Env: envVars,
},
Task: task,
},
)

Expand Down
13 changes: 7 additions & 6 deletions exec/docker_compose_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exec

import (
"context"
"slices"

"github.com/portainer/agent"
libstack "github.com/portainer/portainer/pkg/libstack"
Expand Down Expand Up @@ -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,
})
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 240b826

Please sign in to comment.