-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathconfig.go
327 lines (253 loc) · 7.18 KB
/
config.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package cmd
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
"github.com/deis/pkg/prettyprint"
"github.com/deis/controller-sdk-go/api"
"github.com/deis/controller-sdk-go/config"
)
// ConfigList lists an app's config.
func (d *DeisCmd) ConfigList(appID string, format string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
config, err := config.List(s.Client, appID)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
keys := sortKeys(config.Values)
var configOutput *bytes.Buffer = new(bytes.Buffer)
switch format {
case "oneline":
for i, key := range keys {
sep := " "
if i == len(keys)-1 {
sep = "\n"
}
fmt.Fprintf(configOutput, "%s=%s%s", key, config.Values[key], sep)
}
case "diff":
for _, key := range keys {
fmt.Fprintf(configOutput, "%s=%s\n", key, config.Values[key])
}
default:
fmt.Fprintf(configOutput, "=== %s Config\n", appID)
configMap := make(map[string]string)
// config.Values is type interface, so it needs to be converted to a string
for _, key := range keys {
configMap[key] = fmt.Sprintf("%v", config.Values[key])
}
fmt.Fprint(configOutput, prettyprint.PrettyTabs(configMap, 6))
}
d.Print(configOutput)
return nil
}
// ConfigSet sets an app's config variables.
func (d *DeisCmd) ConfigSet(appID string, configVars []string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
configMap, err := parseConfig(configVars)
if err != nil {
return err
}
if value, ok := configMap["SSH_KEY"]; ok {
sshKey, err := parseSSHKey(value.(string))
if err != nil {
return err
}
configMap["SSH_KEY"] = base64.StdEncoding.EncodeToString([]byte(sshKey))
}
// NOTE(bacongobbler): check if the user is using the old way to set healthchecks. If so,
// send them a deprecation notice.
for key := range configMap {
if strings.Contains(key, "HEALTHCHECK_") {
d.Println(`Hey there! We've noticed that you're using 'deis config:set HEALTHCHECK_URL'
to set up healthchecks. This functionality has been deprecated. In the future, please use
'deis healthchecks' to set up application health checks. Thanks!`)
}
}
d.Print("Creating config... ")
quit := progress(d.WOut)
configObj := api.Config{Values: configMap}
configObj, err = config.Set(s.Client, appID, configObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
if release, ok := configObj.Values["WORKFLOW_RELEASE"]; ok {
d.Printf("done, %s\n\n", release)
} else {
d.Print("done\n\n")
}
return d.ConfigList(appID, "")
}
// ConfigUnset removes a config variable from an app.
func (d *DeisCmd) ConfigUnset(appID string, configVars []string) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
d.Print("Removing config... ")
quit := progress(d.WOut)
configObj := api.Config{}
valuesMap := make(map[string]interface{})
for _, configVar := range configVars {
valuesMap[configVar] = nil
}
configObj.Values = valuesMap
_, err = config.Set(s.Client, appID, configObj)
quit <- true
<-quit
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
d.Print("done\n\n")
return d.ConfigList(appID, "")
}
// ConfigPull pulls an app's config to a file.
func (d *DeisCmd) ConfigPull(appID string, interactive bool, overwrite bool) error {
s, appID, err := load(d.ConfigFile, appID)
if err != nil {
return err
}
configVars, err := config.List(s.Client, appID)
if d.checkAPICompatibility(s.Client, err) != nil {
return err
}
stat, err := os.Stdout.Stat()
if err != nil {
return err
}
if (stat.Mode() & os.ModeCharDevice) == 0 {
d.Print(formatConfig(configVars.Values))
return nil
}
filename := ".env"
if !overwrite {
if _, err := os.Stat(filename); err == nil {
return fmt.Errorf("%s already exists, pass -o to overwrite", filename)
}
}
if interactive {
contents, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
localConfigVars := strings.Split(string(contents), "\n")
configMap, err := parseConfig(localConfigVars[:len(localConfigVars)-1])
if err != nil {
return err
}
for key, value := range configVars.Values {
localValue, ok := configMap[key]
if ok {
if value != localValue {
var confirm string
d.Printf("%s: overwrite %s with %s? (y/N) ", key, localValue, value)
fmt.Scanln(&confirm)
if strings.ToLower(confirm) == "y" {
configMap[key] = value
}
}
} else {
configMap[key] = value
}
}
return ioutil.WriteFile(filename, []byte(formatConfig(configMap)), 0755)
}
return ioutil.WriteFile(filename, []byte(formatConfig(configVars.Values)), 0755)
}
// ConfigPush pushes an app's config from a file.
func (d *DeisCmd) ConfigPush(appID, fileName string) error {
stat, err := os.Stdin.Stat()
if err != nil {
return err
}
var contents []byte
if (stat.Mode() & os.ModeCharDevice) == 0 {
buffer := new(bytes.Buffer)
buffer.ReadFrom(os.Stdin)
contents = buffer.Bytes()
} else {
contents, err = ioutil.ReadFile(fileName)
if err != nil {
return err
}
}
file := strings.Split(string(contents), "\n")
config := []string{}
for _, configVar := range file {
// If file has CRLF encoding, the default on windows, strip the CR
configVar = strings.Trim(configVar, "\r")
if len(configVar) > 0 {
config = append(config, configVar)
}
}
return d.ConfigSet(appID, config)
}
func parseConfig(configVars []string) (map[string]interface{}, error) {
configMap := make(map[string]interface{})
regex := regexp.MustCompile(`^([A-z_]+[A-z0-9_]*)=([\s\S]*)$`)
for _, config := range configVars {
// Skip config that starts with an comment
if config[0] == '#' {
continue
}
if regex.MatchString(config) {
captures := regex.FindStringSubmatch(config)
configMap[captures[1]] = captures[2]
} else {
return nil, fmt.Errorf("'%s' does not match the pattern 'key=var', ex: MODE=test\n", config)
}
}
return configMap, nil
}
func parseSSHKey(value string) (string, error) {
sshRegex := regexp.MustCompile("^-----BEGIN (DSA|RSA|EC|OPENSSH) PRIVATE KEY-----")
if sshRegex.MatchString(value) {
return value, nil
}
// NOTE(felixbuenemann): check if the current value is already a base64 encoded key.
// This is the case if it was fetched using "deis config:pull".
contents, err := base64.StdEncoding.DecodeString(value)
if err == nil && sshRegex.MatchString(string(contents)) {
return string(contents), nil
}
// NOTE(felixbuenemann): check if the value is a path to a private key.
if _, err := os.Stat(value); err == nil {
contents, err := ioutil.ReadFile(value)
if err != nil {
return "", err
}
if sshRegex.MatchString(string(contents)) {
return string(contents), nil
}
}
return "", fmt.Errorf("Could not parse SSH private key:\n %s", value)
}
func formatConfig(configVars map[string]interface{}) string {
var formattedConfig string
keys := sortKeys(configVars)
for _, key := range keys {
formattedConfig += fmt.Sprintf("%s=%v\n", key, configVars[key])
}
return formattedConfig
}
func sortKeys(kv map[string]interface{}) []string {
var keys []string
for k := range kv {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}