X Tutup
Skip to content

Commit a6f19fa

Browse files
Merge pull request docker-archive-public#3839 from s-koba/2046-env-add-api-ver
Add DOCKER_API_VERSION to env
2 parents 64884e8 + cc6c432 commit a6f19fa

File tree

4 files changed

+152
-99
lines changed

4 files changed

+152
-99
lines changed

commands/env.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import (
1313
"github.com/docker/machine/libmachine"
1414
"github.com/docker/machine/libmachine/check"
1515
"github.com/docker/machine/libmachine/log"
16+
"github.com/docker/machine/libmachine/mcndockerclient"
1617
"github.com/docker/machine/libmachine/shell"
1718
)
1819

1920
const (
20-
envTmpl = `{{ .Prefix }}DOCKER_TLS_VERIFY{{ .Delimiter }}{{ .DockerTLSVerify }}{{ .Suffix }}{{ .Prefix }}DOCKER_HOST{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}{{ .Prefix }}DOCKER_CERT_PATH{{ .Delimiter }}{{ .DockerCertPath }}{{ .Suffix }}{{ .Prefix }}DOCKER_MACHINE_NAME{{ .Delimiter }}{{ .MachineName }}{{ .Suffix }}{{ if .ComposePathsVar }}{{ .Prefix }}COMPOSE_CONVERT_WINDOWS_PATHS{{ .Delimiter }}true{{ .Suffix }}{{end}}{{ if .NoProxyVar }}{{ .Prefix }}{{ .NoProxyVar }}{{ .Delimiter }}{{ .NoProxyValue }}{{ .Suffix }}{{end}}{{ .UsageHint }}`
21+
envTmpl = `{{ .Prefix }}DOCKER_TLS_VERIFY{{ .Delimiter }}{{ .DockerTLSVerify }}{{ .Suffix }}{{ .Prefix }}DOCKER_HOST{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}{{ .Prefix }}DOCKER_CERT_PATH{{ .Delimiter }}{{ .DockerCertPath }}{{ .Suffix }}{{ .Prefix }}DOCKER_MACHINE_NAME{{ .Delimiter }}{{ .MachineName }}{{ .Suffix }}{{ .Prefix }}DOCKER_API_VERSION{{ .Delimiter }}{{ .DockerAPIVersion }}{{ .Suffix }}{{ if .ComposePathsVar }}{{ .Prefix }}COMPOSE_CONVERT_WINDOWS_PATHS{{ .Delimiter }}true{{ .Suffix }}{{end}}{{ if .NoProxyVar }}{{ .Prefix }}{{ .NoProxyVar }}{{ .Delimiter }}{{ .NoProxyValue }}{{ .Suffix }}{{end}}{{ .UsageHint }}`
2122
)
2223

2324
var (
@@ -31,17 +32,18 @@ func init() {
3132
}
3233

3334
type ShellConfig struct {
34-
Prefix string
35-
Delimiter string
36-
Suffix string
37-
DockerCertPath string
38-
DockerHost string
39-
DockerTLSVerify string
40-
UsageHint string
41-
MachineName string
42-
NoProxyVar string
43-
NoProxyValue string
44-
ComposePathsVar bool
35+
Prefix string
36+
Delimiter string
37+
Suffix string
38+
DockerCertPath string
39+
DockerHost string
40+
DockerTLSVerify string
41+
DockerAPIVersion string
42+
UsageHint string
43+
MachineName string
44+
NoProxyVar string
45+
NoProxyValue string
46+
ComposePathsVar bool
4547
}
4648

4749
func cmdEnv(c CommandLine, api libmachine.API) error {
@@ -102,6 +104,16 @@ func shellCfgSet(c CommandLine, api libmachine.API) (*ShellConfig, error) {
102104
MachineName: host.Name,
103105
}
104106

107+
remoteDocker := &mcndockerclient.RemoteDocker{
108+
HostURL: dockerHost,
109+
AuthOption: host.AuthOptions(),
110+
}
111+
dockerAPIVersion, err := mcndockerclient.DockerAPIVersion(remoteDocker)
112+
if err != nil {
113+
return nil, err
114+
}
115+
shellCfg.DockerAPIVersion = dockerAPIVersion
116+
105117
if c.Bool("no-proxy") {
106118
ip, err := host.Driver.GetIP()
107119
if err != nil {

commands/env_test.go

Lines changed: 98 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/machine/libmachine/check"
1414
"github.com/docker/machine/libmachine/host"
1515
"github.com/docker/machine/libmachine/libmachinetest"
16+
"github.com/docker/machine/libmachine/mcndockerclient"
1617
"github.com/docker/machine/libmachine/state"
1718
"github.com/stretchr/testify/assert"
1819
)
@@ -96,6 +97,7 @@ func revertUsageHinter(uhg UsageHintGenerator) {
9697
}
9798

9899
func TestShellCfgSet(t *testing.T) {
100+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{APIVersion: "1.21"}
99101
const (
100102
usageHint = "This is a usage hint"
101103
)
@@ -152,15 +154,16 @@ func TestShellCfgSet(t *testing.T) {
152154
Err: nil,
153155
},
154156
expectedShellCfg: &ShellConfig{
155-
Prefix: "export ",
156-
Delimiter: "=\"",
157-
Suffix: "\"\n",
158-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
159-
DockerHost: "tcp://1.2.3.4:2376",
160-
DockerTLSVerify: "1",
161-
UsageHint: usageHint,
162-
MachineName: "quux",
163-
ComposePathsVar: isRuntimeWindows,
157+
Prefix: "export ",
158+
Delimiter: "=\"",
159+
Suffix: "\"\n",
160+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
161+
DockerHost: "tcp://1.2.3.4:2376",
162+
DockerTLSVerify: "1",
163+
DockerAPIVersion: "1.21",
164+
UsageHint: usageHint,
165+
MachineName: "quux",
166+
ComposePathsVar: isRuntimeWindows,
164167
},
165168
expectedErr: nil,
166169
},
@@ -189,15 +192,16 @@ func TestShellCfgSet(t *testing.T) {
189192
Err: nil,
190193
},
191194
expectedShellCfg: &ShellConfig{
192-
Prefix: "export ",
193-
Delimiter: "=\"",
194-
Suffix: "\"\n",
195-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), defaultMachineName),
196-
DockerHost: "tcp://1.2.3.4:2376",
197-
DockerTLSVerify: "1",
198-
UsageHint: usageHint,
199-
MachineName: defaultMachineName,
200-
ComposePathsVar: isRuntimeWindows,
195+
Prefix: "export ",
196+
Delimiter: "=\"",
197+
Suffix: "\"\n",
198+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), defaultMachineName),
199+
DockerHost: "tcp://1.2.3.4:2376",
200+
DockerTLSVerify: "1",
201+
DockerAPIVersion: "1.21",
202+
UsageHint: usageHint,
203+
MachineName: defaultMachineName,
204+
ComposePathsVar: isRuntimeWindows,
201205
},
202206
expectedErr: nil,
203207
},
@@ -226,15 +230,16 @@ func TestShellCfgSet(t *testing.T) {
226230
Err: nil,
227231
},
228232
expectedShellCfg: &ShellConfig{
229-
Prefix: "set -gx ",
230-
Suffix: "\";\n",
231-
Delimiter: " \"",
232-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
233-
DockerHost: "tcp://1.2.3.4:2376",
234-
DockerTLSVerify: "1",
235-
UsageHint: usageHint,
236-
MachineName: "quux",
237-
ComposePathsVar: isRuntimeWindows,
233+
Prefix: "set -gx ",
234+
Suffix: "\";\n",
235+
Delimiter: " \"",
236+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
237+
DockerHost: "tcp://1.2.3.4:2376",
238+
DockerTLSVerify: "1",
239+
DockerAPIVersion: "1.21",
240+
UsageHint: usageHint,
241+
MachineName: "quux",
242+
ComposePathsVar: isRuntimeWindows,
238243
},
239244
expectedErr: nil,
240245
},
@@ -263,15 +268,16 @@ func TestShellCfgSet(t *testing.T) {
263268
Err: nil,
264269
},
265270
expectedShellCfg: &ShellConfig{
266-
Prefix: "$Env:",
267-
Suffix: "\"\n",
268-
Delimiter: " = \"",
269-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
270-
DockerHost: "tcp://1.2.3.4:2376",
271-
DockerTLSVerify: "1",
272-
UsageHint: usageHint,
273-
MachineName: "quux",
274-
ComposePathsVar: isRuntimeWindows,
271+
Prefix: "$Env:",
272+
Suffix: "\"\n",
273+
Delimiter: " = \"",
274+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
275+
DockerHost: "tcp://1.2.3.4:2376",
276+
DockerTLSVerify: "1",
277+
DockerAPIVersion: "1.21",
278+
UsageHint: usageHint,
279+
MachineName: "quux",
280+
ComposePathsVar: isRuntimeWindows,
275281
},
276282
expectedErr: nil,
277283
},
@@ -300,15 +306,16 @@ func TestShellCfgSet(t *testing.T) {
300306
Err: nil,
301307
},
302308
expectedShellCfg: &ShellConfig{
303-
Prefix: "(setenv \"",
304-
Suffix: "\")\n",
305-
Delimiter: "\" \"",
306-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
307-
DockerHost: "tcp://1.2.3.4:2376",
308-
DockerTLSVerify: "1",
309-
UsageHint: usageHint,
310-
MachineName: "quux",
311-
ComposePathsVar: isRuntimeWindows,
309+
Prefix: "(setenv \"",
310+
Suffix: "\")\n",
311+
Delimiter: "\" \"",
312+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
313+
DockerHost: "tcp://1.2.3.4:2376",
314+
DockerTLSVerify: "1",
315+
DockerAPIVersion: "1.21",
316+
UsageHint: usageHint,
317+
MachineName: "quux",
318+
ComposePathsVar: isRuntimeWindows,
312319
},
313320
expectedErr: nil,
314321
},
@@ -337,15 +344,16 @@ func TestShellCfgSet(t *testing.T) {
337344
Err: nil,
338345
},
339346
expectedShellCfg: &ShellConfig{
340-
Prefix: "SET ",
341-
Suffix: "\n",
342-
Delimiter: "=",
343-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
344-
DockerHost: "tcp://1.2.3.4:2376",
345-
DockerTLSVerify: "1",
346-
UsageHint: usageHint,
347-
MachineName: "quux",
348-
ComposePathsVar: isRuntimeWindows,
347+
Prefix: "SET ",
348+
Suffix: "\n",
349+
Delimiter: "=",
350+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
351+
DockerHost: "tcp://1.2.3.4:2376",
352+
DockerTLSVerify: "1",
353+
DockerAPIVersion: "1.21",
354+
UsageHint: usageHint,
355+
MachineName: "quux",
356+
ComposePathsVar: isRuntimeWindows,
349357
},
350358
expectedErr: nil,
351359
},
@@ -378,17 +386,18 @@ func TestShellCfgSet(t *testing.T) {
378386
Err: nil,
379387
},
380388
expectedShellCfg: &ShellConfig{
381-
Prefix: "export ",
382-
Delimiter: "=\"",
383-
Suffix: "\"\n",
384-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
385-
DockerHost: "tcp://1.2.3.4:2376",
386-
DockerTLSVerify: "1",
387-
UsageHint: usageHint,
388-
NoProxyVar: "NO_PROXY",
389-
NoProxyValue: "1.2.3.4", // From FakeDriver
390-
MachineName: "quux",
391-
ComposePathsVar: isRuntimeWindows,
389+
Prefix: "export ",
390+
Delimiter: "=\"",
391+
Suffix: "\"\n",
392+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
393+
DockerHost: "tcp://1.2.3.4:2376",
394+
DockerTLSVerify: "1",
395+
DockerAPIVersion: "1.21",
396+
UsageHint: usageHint,
397+
NoProxyVar: "NO_PROXY",
398+
NoProxyValue: "1.2.3.4", // From FakeDriver
399+
MachineName: "quux",
400+
ComposePathsVar: isRuntimeWindows,
392401
},
393402
noProxyVar: "NO_PROXY",
394403
noProxyValue: "",
@@ -423,17 +432,18 @@ func TestShellCfgSet(t *testing.T) {
423432
Err: nil,
424433
},
425434
expectedShellCfg: &ShellConfig{
426-
Prefix: "export ",
427-
Delimiter: "=\"",
428-
Suffix: "\"\n",
429-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
430-
DockerHost: "tcp://1.2.3.4:2376",
431-
DockerTLSVerify: "1",
432-
UsageHint: usageHint,
433-
NoProxyVar: "no_proxy",
434-
NoProxyValue: "192.168.59.1,1.2.3.4", // From FakeDriver
435-
MachineName: "quux",
436-
ComposePathsVar: isRuntimeWindows,
435+
Prefix: "export ",
436+
Delimiter: "=\"",
437+
Suffix: "\"\n",
438+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
439+
DockerHost: "tcp://1.2.3.4:2376",
440+
DockerTLSVerify: "1",
441+
DockerAPIVersion: "1.21",
442+
UsageHint: usageHint,
443+
NoProxyVar: "no_proxy",
444+
NoProxyValue: "192.168.59.1,1.2.3.4", // From FakeDriver
445+
MachineName: "quux",
446+
ComposePathsVar: isRuntimeWindows,
437447
},
438448
noProxyVar: "no_proxy",
439449
noProxyValue: "192.168.59.1",
@@ -458,6 +468,7 @@ func TestShellCfgSet(t *testing.T) {
458468
}
459469

460470
func TestShellCfgSetWindowsRuntime(t *testing.T) {
471+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{APIVersion: "1.21"}
461472
const (
462473
usageHint = "This is a usage hint"
463474
)
@@ -502,15 +513,16 @@ func TestShellCfgSetWindowsRuntime(t *testing.T) {
502513
Err: nil,
503514
},
504515
expectedShellCfg: &ShellConfig{
505-
Prefix: "$Env:",
506-
Suffix: "\"\n",
507-
Delimiter: " = \"",
508-
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
509-
DockerHost: "tcp://1.2.3.4:2376",
510-
DockerTLSVerify: "1",
511-
UsageHint: usageHint,
512-
MachineName: "quux",
513-
ComposePathsVar: true,
516+
Prefix: "$Env:",
517+
Suffix: "\"\n",
518+
Delimiter: " = \"",
519+
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"),
520+
DockerHost: "tcp://1.2.3.4:2376",
521+
DockerTLSVerify: "1",
522+
DockerAPIVersion: "1.21",
523+
UsageHint: usageHint,
524+
MachineName: "quux",
525+
ComposePathsVar: true,
514526
},
515527
expectedErr: nil,
516528
},
@@ -537,6 +549,7 @@ func TestShellCfgSetWindowsRuntime(t *testing.T) {
537549
}
538550

539551
func TestShellCfgUnset(t *testing.T) {
552+
mcndockerclient.CurrentDockerVersioner = &mcndockerclient.FakeDockerVersioner{APIVersion: "1.21"}
540553
const (
541554
usageHint = "This is the unset usage hint"
542555
)

libmachine/mcndockerclient/docker_versioner.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ var CurrentDockerVersioner DockerVersioner = &defaultDockerVersioner{}
66

77
type DockerVersioner interface {
88
DockerVersion(host DockerHost) (string, error)
9+
DockerAPIVersion(host DockerHost) (string, error)
910
}
1011

1112
func DockerVersion(host DockerHost) (string, error) {
1213
return CurrentDockerVersioner.DockerVersion(host)
1314
}
1415

16+
func DockerAPIVersion(host DockerHost) (string, error) {
17+
return CurrentDockerVersioner.DockerAPIVersion(host)
18+
}
19+
1520
type defaultDockerVersioner struct{}
1621

1722
func (dv *defaultDockerVersioner) DockerVersion(host DockerHost) (string, error) {
@@ -27,3 +32,17 @@ func (dv *defaultDockerVersioner) DockerVersion(host DockerHost) (string, error)
2732

2833
return version.Version, nil
2934
}
35+
36+
func (dv *defaultDockerVersioner) DockerAPIVersion(host DockerHost) (string, error) {
37+
client, err := DockerClient(host)
38+
if err != nil {
39+
return "", fmt.Errorf("Unable to query docker API version: %s", err)
40+
}
41+
42+
version, err := client.Version()
43+
if err != nil {
44+
return "", fmt.Errorf("Unable to query docker API version: %s", err)
45+
}
46+
47+
return version.ApiVersion, nil
48+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package mcndockerclient
22

33
type FakeDockerVersioner struct {
4-
Version string
5-
Err error
4+
Version string
5+
APIVersion string
6+
Err error
67
}
78

89
func (dv *FakeDockerVersioner) DockerVersion(host DockerHost) (string, error) {
@@ -12,3 +13,11 @@ func (dv *FakeDockerVersioner) DockerVersion(host DockerHost) (string, error) {
1213

1314
return dv.Version, nil
1415
}
16+
17+
func (dv *FakeDockerVersioner) DockerAPIVersion(host DockerHost) (string, error) {
18+
if dv.Err != nil {
19+
return "", dv.Err
20+
}
21+
22+
return dv.APIVersion, nil
23+
}

0 commit comments

Comments
 (0)
X Tutup