forked from portainer/compose-unpacker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
undeploy.go
93 lines (75 loc) · 2.16 KB
/
undeploy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"os"
"path"
"runtime"
"strings"
"github.com/portainer/portainer/pkg/libstack"
"github.com/portainer/portainer/pkg/libstack/compose"
"github.com/rs/zerolog/log"
)
func (cmd *UndeployCommand) Run(cmdCtx *CommandExecutionContext) error {
log.Info().
Str("repository", cmd.GitRepository).
Strs("composePath", cmd.ComposeRelativeFilePaths).
Msg("Undeploying Compose stack from Git repository")
i := strings.LastIndex(cmd.GitRepository, "/")
if i == -1 {
log.Error().
Str("repository", cmd.GitRepository).
Msg("Invalid Git repository URL")
return errDeployComposeFailure
}
mountPath := makeWorkingDir(cmd.Destination, cmd.ProjectName)
deployer, err := compose.NewComposeDeployer(BIN_PATH, PORTAINER_DOCKER_CONFIG_PATH)
if err != nil {
log.Error().Err(err).Msg("Failed to create Compose deployer")
return errDeployComposeFailure
}
log.Debug().
Str("projectName", cmd.ProjectName).
Msg("Undeploying Compose stack")
err = deployer.Remove(cmdCtx.context, cmd.ProjectName, nil, libstack.Options{})
if err != nil {
log.Error().
Err(err).
Msg("Failed to remove Compose stack")
return errDeployComposeFailure
}
log.Info().Msg("Compose stack remove complete")
if !cmd.Keep { //stack stop request
err = os.RemoveAll(mountPath)
if err != nil {
log.Error().
Err(err).
Msg("Failed to remove Compose stack project folder")
}
}
return nil
}
func (cmd *SwarmUndeployCommand) Run(cmdCtx *CommandExecutionContext) error {
log.Info().
Str("stack name", cmd.ProjectName).
Str("destination", cmd.Destination).
Msg("Undeploying Swarm stack from Git repository")
command := path.Join(BIN_PATH, "docker")
if runtime.GOOS == "windows" {
command = path.Join(BIN_PATH, "docker.exe")
}
args := make([]string, 0)
args = append(args, "stack", "rm", cmd.ProjectName)
err := runCommandAndCaptureStdErr(command, args, nil, "")
if err != nil {
return err
}
mountPath := makeWorkingDir(cmd.Destination, cmd.ProjectName)
if !cmd.Keep { //stack stop request
err = os.RemoveAll(mountPath)
if err != nil {
log.Error().
Err(err).
Msg("Failed to remove Compose stack project folder")
}
}
return nil
}