@@ -3,14 +3,14 @@ package composeplugin
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "log"
7
6
"os"
8
7
"os/exec"
9
8
"strings"
10
9
11
10
"github.com/pkg/errors"
12
11
libstack "github.com/portainer/docker-compose-wrapper"
13
12
"github.com/portainer/docker-compose-wrapper/compose/internal/utils"
13
+ "github.com/rs/zerolog/log"
14
14
)
15
15
16
16
var (
@@ -33,64 +33,81 @@ func NewPluginWrapper(binaryPath, configPath string) (libstack.Deployer, error)
33
33
}
34
34
35
35
// Up create and start containers
36
- func (wrapper * PluginWrapper ) Deploy (ctx context.Context , workingDir , host , projectName string , filePaths []string , envFilePath string , forceRecreate bool ) error {
37
- output , err := wrapper .command (newUpCommand (filePaths , forceRecreate ), workingDir , host , projectName , envFilePath )
36
+ func (wrapper * PluginWrapper ) Deploy (ctx context.Context , filePaths []string , options libstack.DeployOptions ) error {
37
+ output , err := wrapper .command (newUpCommand (filePaths , upOptions {
38
+ forceRecreate : options .ForceRecreate ,
39
+ abortOnContainerExit : options .AbortOnContainerExit ,
40
+ }), options .Options ,
41
+ )
38
42
if len (output ) != 0 {
39
43
if err != nil {
40
44
return err
41
45
}
42
46
43
- log .Printf ("[INFO] [docker compose] [message: Stack deployment successful]" )
44
- log .Printf ("[DEBUG] [docker compose] [output: %s]" , output )
47
+ log .Info ().
48
+ Str ("message" , "Stack deployment successful" )
49
+
50
+ log .Debug ().
51
+ Str ("output" , string (output )).
52
+ Msg ("docker compose" )
45
53
}
46
54
47
55
return err
48
56
}
49
57
50
58
// Down stop and remove containers
51
- func (wrapper * PluginWrapper ) Remove (ctx context.Context , workingDir , host , projectName string , filePaths []string , envFilePath string ) error {
52
- output , err := wrapper .command (newDownCommand (filePaths ), workingDir , host , projectName , envFilePath )
59
+ func (wrapper * PluginWrapper ) Remove (ctx context.Context , filePaths []string , options libstack. Options ) error {
60
+ output , err := wrapper .command (newDownCommand (filePaths ), options )
53
61
if len (output ) != 0 {
54
62
if err != nil {
55
63
return err
56
64
}
57
65
58
- log .Printf ("[INFO] [docker compose] [message: Stack removal successful]" )
59
- log .Printf ("[DEBUG] [docker compose] [output: %s]" , output )
66
+ log .Info ().
67
+ Str ("message" , "Stack removal successful" )
68
+
69
+ log .Debug ().
70
+ Str ("output" , string (output )).
71
+ Msg ("docker compose" )
72
+
60
73
}
61
74
62
75
return err
63
76
}
64
77
65
78
// Pull images
66
- func (wrapper * PluginWrapper ) Pull (ctx context.Context , workingDir , host , projectName string , filePaths []string , envFilePath string ) error {
67
- output , err := wrapper .command (newPullCommand (filePaths ), workingDir , host , projectName , envFilePath )
79
+ func (wrapper * PluginWrapper ) Pull (ctx context.Context , filePaths []string , options libstack. Options ) error {
80
+ output , err := wrapper .command (newPullCommand (filePaths ), options )
68
81
if len (output ) != 0 {
69
82
if err != nil {
70
83
return err
71
84
}
72
85
73
- log .Printf ("[INFO] [docker compose] [message: Stack pull successful]" )
74
- log .Printf ("[DEBUG] [docker compose] [output: %s]" , output )
86
+ log .Info ().
87
+ Str ("message" , "Stack pull successful" )
88
+
89
+ log .Debug ().
90
+ Str ("output" , string (output )).
91
+ Msg ("docker compose" )
75
92
}
76
93
77
94
return err
78
95
}
79
96
80
97
// Command execute a docker-compose command
81
- func (wrapper * PluginWrapper ) command (command composeCommand , workingDir , host , projectName , envFilePath string ) ([]byte , error ) {
98
+ func (wrapper * PluginWrapper ) command (command composeCommand , options libstack. Options ) ([]byte , error ) {
82
99
program := utils .ProgramPath (wrapper .binaryPath , "docker-compose" )
83
100
84
- if projectName != "" {
85
- command .WithProjectName (projectName )
101
+ if options . ProjectName != "" {
102
+ command .WithProjectName (options . ProjectName )
86
103
}
87
104
88
- if envFilePath != "" {
89
- command .WithEnvFilePath (envFilePath )
105
+ if options . EnvFilePath != "" {
106
+ command .WithEnvFilePath (options . EnvFilePath )
90
107
}
91
108
92
- if host != "" {
93
- command .WithHost (host )
109
+ if options . Host != "" {
110
+ command .WithHost (options . Host )
94
111
}
95
112
96
113
var stderr bytes.Buffer
@@ -99,20 +116,26 @@ func (wrapper *PluginWrapper) command(command composeCommand, workingDir, host,
99
116
args = append (args , command .ToArgs ()... )
100
117
101
118
cmd := exec .Command (program , args ... )
102
- cmd .Dir = workingDir
119
+ cmd .Dir = options . WorkingDir
103
120
104
121
if wrapper .configPath != "" {
105
- if wrapper .configPath != "" {
106
- cmd .Env = os .Environ ()
107
- cmd .Env = append (cmd .Env , "DOCKER_CONFIG=" + wrapper .configPath )
108
- }
122
+ cmd .Env = os .Environ ()
123
+ cmd .Env = append (cmd .Env , "DOCKER_CONFIG=" + wrapper .configPath )
109
124
}
110
125
111
126
cmd .Stderr = & stderr
112
127
113
128
output , err := cmd .Output ()
114
129
if err != nil {
115
- return nil , errors .New (stderr .String ())
130
+ errOutput := stderr .String ()
131
+ log .Warn ().
132
+ Str ("output" , string (output )).
133
+ Str ("error_output" , errOutput ).
134
+ Err (err ).
135
+ Msg ("docker compose command failed" )
136
+
137
+ // stderr output outputs useless information such as "Removing network stack_default"
138
+ return nil , errors .WithMessage (err , "docker-compose command failed" )
116
139
}
117
140
118
141
return output , nil
@@ -135,9 +158,21 @@ func newCommand(command []string, filePaths []string) composeCommand {
135
158
}
136
159
}
137
160
138
- func newUpCommand (filePaths []string , forceRereate bool ) composeCommand {
139
- args := []string {"up" , "-d" }
140
- if forceRereate {
161
+ type upOptions struct {
162
+ forceRecreate bool
163
+ abortOnContainerExit bool ``
164
+ }
165
+
166
+ func newUpCommand (filePaths []string , options upOptions ) composeCommand {
167
+ args := []string {"up" }
168
+
169
+ if options .abortOnContainerExit {
170
+ args = append (args , "--abort-on-container-exit" )
171
+ } else { // detach by default, not working with --abort-on-container-exit
172
+ args = append (args , "-d" )
173
+ }
174
+
175
+ if options .forceRecreate {
141
176
args = append (args , "--force-recreate" )
142
177
}
143
178
return newCommand (args , filePaths )
0 commit comments