Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd): allow to pass forceRecreate setting [EE-7374] #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ func (cmd *DeployCommand) Run(cmdCtx *CommandExecutionContext) error {
Bool("skipTLSVerify", cmd.SkipTLSVerify).
Msg("Deploying Compose stack from Git repository")

defer dockerLogout(cmd.Registry)
err := dockerLogin(cmd.Registry)
if err != nil {
return err
if err := dockerLogin(cmd.Registry); err != nil {
return fmt.Errorf("an error occured in docker login. Error: %w", err)
}
defer dockerLogout(cmd.Registry)

if cmd.User != "" && cmd.Password != "" {
log.Info().
Expand Down Expand Up @@ -131,7 +130,7 @@ func (cmd *DeployCommand) Run(cmdCtx *CommandExecutionContext) error {
ProjectName: cmd.ProjectName,
Env: cmd.Env,
},
ForceRecreate: true,
ForceRecreate: cmd.ForceRecreateStack,
})

if err != nil {
Expand All @@ -152,11 +151,11 @@ func (cmd *SwarmDeployCommand) Run(cmdCtx *CommandExecutionContext) error {
Str("destination", cmd.Destination).
Msg("Deploying Swarm stack from a Git repository")

defer dockerLogout(cmd.Registry)
err := dockerLogin(cmd.Registry)
if err != nil {
return err
return fmt.Errorf("an error occured in swarm docker login. Error: %w", err)
}
defer dockerLogout(cmd.Registry)

if cmd.User != "" && cmd.Password != "" {
log.Info().
Expand Down Expand Up @@ -193,7 +192,7 @@ func (cmd *SwarmDeployCommand) Run(cmdCtx *CommandExecutionContext) error {
}

forceUpdate := false
if len(runningServices) > 0 {
if cmd.ForceRecreateStack && len(runningServices) > 0 {
// To determine whether the current service needs to force update, it
// is more reliable to check if there is a created service with the
// stack name rather than to check if there is an existing git repository.
Expand Down Expand Up @@ -262,9 +261,8 @@ func (cmd *SwarmDeployCommand) Run(cmdCtx *CommandExecutionContext) error {
}

for _, updatedServiceID := range updatedServiceIDs {
_, ok := runningServices[updatedServiceID]
if ok {
_ = updateService(updatedServiceID)
if _, ok := runningServices[updatedServiceID]; ok {
_ = updateService(updatedServiceID, forceUpdate)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.5
require (
github.com/alecthomas/kong v0.6.1
github.com/go-git/go-git/v5 v5.11.0
github.com/portainer/portainer/pkg/libstack v0.0.0-20230626042119-89c1d0e33707
github.com/portainer/portainer/pkg/libstack v0.0.0-20230831224222-4560a53317b0
github.com/rs/zerolog v1.28.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230626042119-89c1d0e33707 h1:SmwWcOqHpbHj+4tZNR9AV4VfLWmbelkaAcWYuMkQ9G8=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230626042119-89c1d0e33707/go.mod h1:+zCK2UbsH6A3yEGi0yZ45ec5VFRP7svob5Q2lW6LFgk=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230831224222-4560a53317b0 h1:BsSUEHzLdwxQ32QS5qdFYkWT3e7SqCLfrdeW69s1Ku4=
github.com/portainer/portainer/pkg/libstack v0.0.0-20230831224222-4560a53317b0/go.mod h1:+zCK2UbsH6A3yEGi0yZ45ec5VFRP7svob5Q2lW6LFgk=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand Down
15 changes: 12 additions & 3 deletions swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func deploySwarmStack(cmd SwarmDeployCommand, clonePath string) error {
} else {
args = append(args, "stack", "deploy", "--with-registry-auth")
}

if !cmd.Pull {
args = append(args, "--resolve-image=never")
}
Expand Down Expand Up @@ -68,22 +69,30 @@ func checkRunningService(projectName string) ([]string, error) {
return serviceIDs, nil
}

func updateService(serviceID string) error {
func updateService(serviceID string, forceRecreate bool) error {
command := getDockerBinaryPath()
args := []string{"--config", PORTAINER_DOCKER_CONFIG_PATH, "service", "update", serviceID, "--force"}
args := []string{"--config", PORTAINER_DOCKER_CONFIG_PATH, "service", "update", serviceID}
if forceRecreate {
args = append(args, "--force")
}

log.Info().
Strs("args", args).
Msg("Updating Swarm service")
_, err := runCommand(command, args)

out, err := runCommand(command, args)
if err != nil {
log.Error().
Err(err).
Str("standard_output", out).
Str("context", "SwarmDeployerUpdateService").
Msg("Failed to update swarm services")
return err
}

log.Info().
Str("standard_output", out).
Str("context", "SwarmDeployerUpdateService").
Msg("Update stack service completed")
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type DeployCommand struct {
Password string `help:"Password or PAT for Git authentication" short:"p"`
Keep bool `help:"Keep stack folder" short:"k"`
SkipTLSVerify bool `help:"Skip TLS verification for git" name:"skip-tls-verify"`
ForceRecreateStack bool `help:"Force to recreate the target stack regardless whether the image hash changes" name:"force-recreate"`
Env []string `help:"OS ENV for stack" example:"key=value"`
Registry []string `help:"Registry credentials" name:"registry"`
GitRepository string `arg:"" help:"Git repository to deploy from." name:"git-repo"`
Expand All @@ -39,6 +40,7 @@ type SwarmDeployCommand struct {
Prune bool `help:"Prune services during deployment" short:"r"`
Keep bool `help:"Keep stack folder" short:"k"`
SkipTLSVerify bool `help:"Skip TLS verification for git" name:"skip-tls-verify"`
ForceRecreateStack bool `help:"Force to recreate the target stack regardless whether the image hash changes" name:"force-recreate"`
Env []string `help:"OS ENV for stack."`
Registry []string `help:"Registry credentials" name:"registry"`
GitRepository string `arg:"" help:"Git repository to deploy from." name:"git-repo"`
Expand Down