forked from minishift/minishift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
181 lines (145 loc) · 5.45 KB
/
env.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Part of this code is heavily inspired/copied by the following file:
// github.com/docker/machine/commands/env.go
package cmd
import (
"fmt"
"os"
"text/template"
"github.com/minishift/minishift/pkg/minikube/constants"
"strings"
"github.com/docker/machine/libmachine"
"github.com/minishift/minishift/cmd/minishift/cmd/util"
"github.com/minishift/minishift/pkg/minikube/cluster"
"github.com/minishift/minishift/pkg/util/os/atexit"
"github.com/minishift/minishift/pkg/util/shell"
"github.com/spf13/cobra"
)
const (
envTmpl = `{{ .Prefix }}DOCKER_TLS_VERIFY{{ .Delimiter }}{{ .DockerTLSVerify }}{{ .Suffix }}{{ .Prefix }}DOCKER_HOST{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}{{ .Prefix }}DOCKER_CERT_PATH{{ .Delimiter }}{{ .DockerCertPath }}{{ .Suffix }}{{ .Prefix }}DOCKER_API_VERSION{{ .Delimiter }}{{ .DockerAPIVersion }}{{ .Suffix }}{{ if .NoProxyVar }}{{ .Prefix }}{{ .NoProxyVar }}{{ .Delimiter }}{{ .NoProxyValue }}{{ .Suffix }}{{end}}{{ .UsageHint }}`
)
var (
noProxy bool
forceShell string
unset bool
)
type DockerShellConfig struct {
shell.ShellConfig
DockerCertPath string
DockerHost string
DockerTLSVerify string
DockerAPIVersion string
UsageHint string
NoProxyVar string
NoProxyValue string
}
func getConfigSet(api libmachine.API, forceShell string, noProxy bool) (*DockerShellConfig, error) {
envMap, err := cluster.GetHostDockerEnv(api)
if err != nil {
return nil, err
}
userShell, err := shell.GetShell(forceShell)
if err != nil {
return nil, err
}
cmdLine := "minishift docker-env"
shellCfg := &DockerShellConfig{
DockerCertPath: envMap["DOCKER_CERT_PATH"],
DockerHost: envMap["DOCKER_HOST"],
DockerTLSVerify: envMap["DOCKER_TLS_VERIFY"],
DockerAPIVersion: envMap["DOCKER_API_VERSION"],
UsageHint: shell.GenerateUsageHint(userShell, cmdLine),
}
if noProxy {
host, err := api.Load(constants.MachineName)
if err != nil {
return nil, fmt.Errorf("Error getting IP: %s", err)
}
ip, err := host.Driver.GetIP()
if err != nil {
return nil, fmt.Errorf("Error getting host IP: %s", err)
}
noProxyVar, noProxyValue := shell.FindNoProxyFromEnv()
// add the docker host to the no_proxy list idempotently
switch {
case noProxyValue == "":
noProxyValue = ip
case strings.Contains(noProxyValue, ip):
//ip already in no_proxy list, nothing to do
default:
noProxyValue = fmt.Sprintf("%s,%s", noProxyValue, ip)
}
shellCfg.NoProxyVar = noProxyVar
shellCfg.NoProxyValue = noProxyValue
}
shellCfg.Prefix, shellCfg.Suffix, shellCfg.Delimiter = shell.GetPrefixSuffixDelimiterForSet(userShell, false)
return shellCfg, nil
}
func getConfigUnset(forceShell string, noProxy bool) (*DockerShellConfig, error) {
userShell, err := shell.GetShell(forceShell)
if err != nil {
return nil, err
}
cmdLine := "minishift docker-env"
shellCfg := &DockerShellConfig{
UsageHint: shell.GenerateUsageHint(userShell, cmdLine),
}
if noProxy {
shellCfg.NoProxyVar, shellCfg.NoProxyValue = shell.FindNoProxyFromEnv()
}
prefix, suffix, delimiter := shell.GetPrefixSuffixDelimiterForUnSet(userShell)
shellCfg.Prefix = prefix
shellCfg.Suffix = suffix
shellCfg.Delimiter = delimiter
return shellCfg, nil
}
func executeTemplateStdout(shellCfg *DockerShellConfig) error {
tmpl := template.Must(template.New("envConfig").Parse(envTmpl))
return tmpl.Execute(os.Stdout, shellCfg)
}
// envCmd represents the docker-env command
var dockerEnvCmd = &cobra.Command{
Use: "docker-env",
Short: "Sets Docker environment variables.",
Long: `Sets Docker environment variables, similar to '$(docker-machine env)'.`,
Run: func(cmd *cobra.Command, args []string) {
api := libmachine.NewClient(constants.Minipath, constants.MakeMiniPath("certs"))
defer api.Close()
util.ExitIfUndefined(api, constants.MachineName)
host, err := api.Load(constants.MachineName)
if err != nil {
atexit.ExitWithMessage(1, err.Error())
}
util.ExitIfNotRunning(host.Driver, constants.MachineName)
var shellCfg *DockerShellConfig
if unset {
shellCfg, err = getConfigUnset(forceShell, noProxy)
if err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error unsetting environment variables: %s", err.Error()))
}
} else {
shellCfg, err = getConfigSet(api, forceShell, noProxy)
if err != nil {
atexit.ExitWithMessage(1, fmt.Sprintf("Error setting environment variables: %s", err.Error()))
}
}
executeTemplateStdout(shellCfg)
},
}
func init() {
RootCmd.AddCommand(dockerEnvCmd)
dockerEnvCmd.Flags().BoolVar(&noProxy, "no-proxy", false, "Add the virtual machine IP to the NO_PROXY environment variable.")
dockerEnvCmd.Flags().StringVar(&forceShell, "shell", "", "Force setting the environment for a specified shell: [fish, cmd, powershell, tcsh, bash, zsh]. Default is auto-detect.")
dockerEnvCmd.Flags().BoolVarP(&unset, "unset", "u", false, "Clear the environment variable values instead of setting them.")
}