forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
200 lines (169 loc) · 4.46 KB
/
cmd.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package helm
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"regexp"
"github.com/argoproj/argo-cd/util"
executil "github.com/argoproj/argo-cd/util/exec"
)
// A thin wrapper around the "helm" command, adding logging and error translation.
type Cmd struct {
helmHome string
WorkDir string
}
func NewCmd(workDir string) (*Cmd, error) {
tmpDir, err := ioutil.TempDir("", "helm")
if err != nil {
return nil, err
}
return &Cmd{WorkDir: workDir, helmHome: tmpDir}, err
}
var redactor = func(text string) string {
return regexp.MustCompile("(--username|--password) [^ ]*").ReplaceAllString(text, "$1 ******")
}
func (c Cmd) run(args ...string) (string, error) {
cmd := exec.Command("helm", args...)
cmd.Dir = c.WorkDir
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("HELM_HOME=%s", c.helmHome))
return executil.RunWithRedactor(cmd, redactor)
}
func (c *Cmd) Init() (string, error) {
return c.run("init", "--client-only", "--skip-refresh")
}
func (c *Cmd) RepoAdd(name, url string, opts Creds) (string, error) {
tmp, err := ioutil.TempDir("", "helm")
if err != nil {
return "", err
}
defer func() { _ = os.RemoveAll(tmp) }()
args := []string{"repo", "add"}
if opts.Username != "" {
args = append(args, "--username", opts.Username)
}
if opts.Password != "" {
args = append(args, "--password", opts.Password)
}
if opts.CAPath != "" {
args = append(args, "--ca-file", opts.CAPath)
}
if len(opts.CertData) > 0 {
certFile, err := ioutil.TempFile("", "helm")
if err != nil {
return "", err
}
_, err = certFile.Write(opts.CertData)
if err != nil {
return "", err
}
args = append(args, "--cert-file", certFile.Name())
}
if len(opts.KeyData) > 0 {
keyFile, err := ioutil.TempFile("", "helm")
if err != nil {
return "", err
}
_, err = keyFile.Write(opts.KeyData)
if err != nil {
return "", err
}
args = append(args, "--key-file", keyFile.Name())
}
args = append(args, name, url)
return c.run(args...)
}
func (c *Cmd) RepoUpdate() (string, error) {
return c.run("repo", "update")
}
func writeToTmp(data []byte) (string, io.Closer, error) {
file, err := ioutil.TempFile("", "")
if err != nil {
return "", nil, err
}
err = ioutil.WriteFile(file.Name(), data, 0644)
if err != nil {
_ = os.RemoveAll(file.Name())
return "", nil, err
}
return file.Name(), util.NewCloser(func() error {
return os.RemoveAll(file.Name())
}), nil
}
func (c *Cmd) Fetch(repo, chartName, version, destination string, creds Creds) (string, error) {
args := []string{"fetch", "--destination", destination}
if version != "" {
args = append(args, "--version", version)
}
if creds.Username != "" {
args = append(args, "--username", creds.Username)
}
if creds.Password != "" {
args = append(args, "--password", creds.Password)
}
if creds.CAPath != "" {
args = append(args, "--ca-file", creds.CAPath)
}
if len(creds.CertData) > 0 {
filePath, closer, err := writeToTmp(creds.CertData)
if err != nil {
return "", err
}
defer util.Close(closer)
args = append(args, "--cert-file", filePath)
}
if len(creds.KeyData) > 0 {
filePath, closer, err := writeToTmp(creds.KeyData)
if err != nil {
return "", err
}
defer util.Close(closer)
args = append(args, "--key-file", filePath)
}
args = append(args, "--repo", repo, chartName)
return c.run(args...)
}
func (c *Cmd) dependencyBuild() (string, error) {
return c.run("dependency", "build")
}
func (c *Cmd) inspectValues(values string) (string, error) {
return c.run("inspect", "values", values)
}
type TemplateOpts struct {
Name string
Namespace string
KubeVersion string
Set map[string]string
SetString map[string]string
Values []string
}
var (
re = regexp.MustCompile(`([^\\]),`)
)
func cleanSetParameters(val string) string {
return re.ReplaceAllString(val, `$1\,`)
}
func (c *Cmd) template(chart string, opts *TemplateOpts) (string, error) {
args := []string{"template", chart, "--name", opts.Name}
if opts.Namespace != "" {
args = append(args, "--namespace", opts.Namespace)
}
if opts.KubeVersion != "" {
args = append(args, "--kube-version", opts.KubeVersion)
}
for key, val := range opts.Set {
args = append(args, "--set", key+"="+cleanSetParameters(val))
}
for key, val := range opts.SetString {
args = append(args, "--set-string", key+"="+cleanSetParameters(val))
}
for _, val := range opts.Values {
args = append(args, "--values", val)
}
return c.run(args...)
}
func (c *Cmd) Close() {
_ = os.RemoveAll(c.helmHome)
}