-
Notifications
You must be signed in to change notification settings - Fork 7
/
swarm.go
117 lines (98 loc) · 2.64 KB
/
swarm.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"path"
"runtime"
"strings"
"github.com/rs/zerolog/log"
)
func deploySwarmStack(cmd SwarmDeployCommand, clonePath string) error {
command := getDockerBinaryPath()
args := []string{"--config", PORTAINER_DOCKER_CONFIG_PATH}
if cmd.Prune {
args = append(args, "stack", "deploy", "--prune", "--with-registry-auth")
} else {
args = append(args, "stack", "deploy", "--with-registry-auth")
}
if !cmd.Pull {
args = append(args, "--resolve-image=never")
}
for _, cfile := range cmd.ComposeRelativeFilePaths {
args = append(args, "--compose-file", path.Join(clonePath, cfile))
}
log.Info().
Strs("composeFilePaths", cmd.ComposeRelativeFilePaths).
Str("workingDirectory", clonePath).
Str("projectName", cmd.ProjectName).
Msg("Deploying Swarm stack")
args = append(args, cmd.ProjectName)
err := runCommandAndCaptureStdErr(command, args, cmd.Env, clonePath)
if err != nil {
log.Error().
Err(err).
Msg("Failed to swarm deploy Git repository")
return errDeployComposeFailure
}
log.Info().
Msg("Swarm stack deployment complete")
return err
}
func checkRunningService(projectName string) ([]string, error) {
command := getDockerBinaryPath()
args := []string{"--config", PORTAINER_DOCKER_CONFIG_PATH, "stack", "services", "--format={{.ID}}", projectName}
log.Info().
Strs("args", args).
Msg("Checking Swarm stack")
output, err := runCommand(command, args)
if err != nil {
log.Error().
Err(err).
Msg("Failed to check running swarm services")
return nil, err
}
serviceIDs := splitLines(string(output))
log.Info().
Strs("serviceIDs", serviceIDs).
Msg("Checking stack services")
return serviceIDs, nil
}
func updateService(serviceID string, forceRecreate bool) error {
command := getDockerBinaryPath()
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")
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
}
func splitLines(s string) []string {
var separator string
if runtime.GOOS == "windows" {
separator = "\r\n"
} else {
separator = "\n"
}
parts := strings.Split(s, separator)
ret := []string{}
for _, part := range parts {
// remove empty string
if part != "" {
ret = append(ret, part)
}
}
return ret
}