@@ -6,12 +6,10 @@ import (
6
6
"log"
7
7
"os"
8
8
"os/exec"
9
- "path"
10
9
"strings"
11
10
12
11
"github.com/pkg/errors"
13
12
libstack "github.com/portainer/docker-compose-wrapper"
14
- liberrors "github.com/portainer/docker-compose-wrapper/compose/errors"
15
13
"github.com/portainer/docker-compose-wrapper/compose/internal/utils"
16
14
)
17
15
@@ -27,50 +25,23 @@ type PluginWrapper struct {
27
25
28
26
// NewPluginWrapper initializes a new ComposeWrapper service with local docker-compose binary.
29
27
func NewPluginWrapper (binaryPath , configPath string ) (libstack.Deployer , error ) {
30
- if ! utils .IsBinaryPresent (utils .ProgramPath (binaryPath , "docker" )) {
31
- return nil , liberrors .ErrBinaryNotFound
32
- }
33
-
34
- if configPath == "" {
35
- homePath , err := os .UserHomeDir ()
36
- if err != nil {
37
- return nil , errors .WithMessage (err , "failed fetching user home directory" )
38
- }
39
- configPath = path .Join (homePath , ".docker" )
40
- }
41
-
42
- dockerPluginsPath := path .Join (configPath , "cli-plugins" )
43
- pluginPath := utils .ProgramPath (binaryPath , "docker-compose.plugin" )
44
-
45
- if utils .IsBinaryPresent (pluginPath ) {
46
- if ! utils .IsBinaryPresent (utils .ProgramPath (dockerPluginsPath , "docker-compose" )) {
47
- err := os .MkdirAll (dockerPluginsPath , 0755 )
48
- if err != nil {
49
- return nil , errors .WithMessage (err , "failed creating plugins path" )
50
- }
51
- }
52
-
53
- err := utils .Move (pluginPath , utils .ProgramPath (dockerPluginsPath , "docker-compose" ))
54
- if err != nil {
55
- return nil , err
56
- }
57
- } else if ! utils .IsBinaryPresent (utils .ProgramPath (dockerPluginsPath , "docker-compose" )) {
28
+ if ! utils .IsBinaryPresent (utils .ProgramPath (binaryPath , "docker-compose" )) {
58
29
return nil , MissingDockerComposePluginErr
59
30
}
60
31
61
32
return & PluginWrapper {binaryPath : binaryPath , configPath : configPath }, nil
62
33
}
63
34
64
35
// Up create and start containers
65
- func (wrapper * PluginWrapper ) Deploy (ctx context.Context , workingDir , host , projectName string , filePaths []string , envFilePath string , forceRereate bool ) error {
66
- output , err := wrapper .command (newUpCommand (filePaths , forceRereate ), workingDir , host , projectName , envFilePath )
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 )
67
38
if len (output ) != 0 {
68
39
if err != nil {
69
40
return err
70
41
}
71
42
72
- log .Printf ("[INFO] [compose,internal,composeplugin ] [message: Stack deployment successful]" )
73
- log .Printf ("[DEBUG] [compose,internal,composeplugin ] [output: %s]" , output )
43
+ log .Printf ("[INFO] [docker compose] [message: Stack deployment successful]" )
44
+ log .Printf ("[DEBUG] [docker compose] [output: %s]" , output )
74
45
}
75
46
76
47
return err
@@ -84,8 +55,8 @@ func (wrapper *PluginWrapper) Remove(ctx context.Context, workingDir, host, proj
84
55
return err
85
56
}
86
57
87
- log .Printf ("[INFO] [compose,internal,composeplugin ] [message: Stack removal successful]" )
88
- log .Printf ("[DEBUG] [compose,internal,composeplugin ] [output: %s]" , output )
58
+ log .Printf ("[INFO] [docker compose] [message: Stack removal successful]" )
59
+ log .Printf ("[DEBUG] [docker compose] [output: %s]" , output )
89
60
}
90
61
91
62
return err
@@ -99,16 +70,16 @@ func (wrapper *PluginWrapper) Pull(ctx context.Context, workingDir, host, projec
99
70
return err
100
71
}
101
72
102
- log .Printf ("[INFO] [compose,internal,composeplugin ] [message: Stack pull successful]" )
103
- log .Printf ("[DEBUG] [compose,internal,composeplugin ] [output: %s]" , output )
73
+ log .Printf ("[INFO] [docker compose] [message: Stack pull successful]" )
74
+ log .Printf ("[DEBUG] [docker compose] [output: %s]" , output )
104
75
}
105
76
106
77
return err
107
78
}
108
79
109
- // Command exectue a docker-compose commanåd
110
- func (wrapper * PluginWrapper ) command (command composeCommand , workingDir , url , projectName , envFilePath string ) ([]byte , error ) {
111
- program := utils .ProgramPath (wrapper .binaryPath , "docker" )
80
+ // Command execute a docker-compose command
81
+ func (wrapper * PluginWrapper ) command (command composeCommand , workingDir , host , projectName , envFilePath string ) ([]byte , error ) {
82
+ program := utils .ProgramPath (wrapper .binaryPath , "docker-compose " )
112
83
113
84
if projectName != "" {
114
85
command .WithProjectName (projectName )
@@ -118,24 +89,25 @@ func (wrapper *PluginWrapper) command(command composeCommand, workingDir, url, p
118
89
command .WithEnvFilePath (envFilePath )
119
90
}
120
91
121
- var stderr bytes.Buffer
122
-
123
- args := []string {}
124
-
125
- if wrapper .configPath != "" {
126
- args = append (args , "--config" , wrapper .configPath )
92
+ if host != "" {
93
+ command .WithHost (host )
127
94
}
128
95
129
- if url != "" {
130
- args = append (args , "-H" , url )
131
- }
96
+ var stderr bytes.Buffer
132
97
133
- args = append ( args , "compose" )
98
+ args := [] string {}
134
99
args = append (args , command .ToArgs ()... )
135
100
136
101
cmd := exec .Command (program , args ... )
137
102
cmd .Dir = workingDir
138
103
104
+ if wrapper .configPath != "" {
105
+ if wrapper .configPath != "" {
106
+ cmd .Env = os .Environ ()
107
+ cmd .Env = append (cmd .Env , "DOCKER_CONFIG=" + wrapper .configPath )
108
+ }
109
+ }
110
+
139
111
cmd .Stderr = & stderr
140
112
141
113
output , err := cmd .Output ()
@@ -147,19 +119,19 @@ func (wrapper *PluginWrapper) command(command composeCommand, workingDir, url, p
147
119
}
148
120
149
121
type composeCommand struct {
150
- command []string
151
- args []string
122
+ globalArgs []string // docker-compose global arguments: --host host -f file.yaml
123
+ subCommandAndArgs []string // docker-compose subcommand: up, down folllowed by subcommand arguments
152
124
}
153
125
154
126
func newCommand (command []string , filePaths []string ) composeCommand {
155
- var args []string
127
+ args := []string {}
156
128
for _ , path := range filePaths {
157
129
args = append (args , "-f" )
158
130
args = append (args , strings .TrimSpace (path ))
159
131
}
160
132
return composeCommand {
161
- args : args ,
162
- command : command ,
133
+ globalArgs : args ,
134
+ subCommandAndArgs : command ,
163
135
}
164
136
}
165
137
@@ -179,22 +151,20 @@ func newPullCommand(filePaths []string) composeCommand {
179
151
return newCommand ([]string {"pull" }, filePaths )
180
152
}
181
153
182
- func (command * composeCommand ) WithConfigPath (configPath string ) {
183
- command .args = append (command .args , "--config" , configPath )
154
+ func (command * composeCommand ) WithHost (host string ) {
155
+ // prepend compatibility flags such as this one as they must appear before the
156
+ // regular global args otherwise docker-compose will throw an error
157
+ command .globalArgs = append ([]string {"--host" , host }, command .globalArgs ... )
184
158
}
185
159
186
160
func (command * composeCommand ) WithProjectName (projectName string ) {
187
- command .args = append (command .args , "-p " , projectName )
161
+ command .globalArgs = append (command .globalArgs , "--project-name " , projectName )
188
162
}
189
163
190
164
func (command * composeCommand ) WithEnvFilePath (envFilePath string ) {
191
- command .args = append (command .args , "--env-file" , envFilePath )
192
- }
193
-
194
- func (command * composeCommand ) WithURL (url string ) {
195
- command .args = append (command .args , "-H" , url )
165
+ command .globalArgs = append (command .globalArgs , "--env-file" , envFilePath )
196
166
}
197
167
198
168
func (command * composeCommand ) ToArgs () []string {
199
- return append (command .args , command .command ... )
169
+ return append (command .globalArgs , command .subCommandAndArgs ... )
200
170
}
0 commit comments