-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathcmd_config.go
347 lines (299 loc) · 10.6 KB
/
cmd_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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
toml "github.com/yesnault/go-toml"
"github.com/ovh/cds/engine/api"
"github.com/ovh/cds/engine/cdn"
"github.com/ovh/cds/engine/hatchery/kubernetes"
"github.com/ovh/cds/engine/hatchery/local"
"github.com/ovh/cds/engine/hatchery/openstack"
"github.com/ovh/cds/engine/hatchery/swarm"
"github.com/ovh/cds/engine/hatchery/vsphere"
"github.com/ovh/cds/engine/hooks"
"github.com/ovh/cds/engine/ui"
"github.com/ovh/cds/engine/vcs"
"github.com/ovh/cds/sdk"
)
func init() {
configCmd.AddCommand(configNewCmd)
configCmd.AddCommand(configCheckCmd)
configCmd.AddCommand(configRegenCmd)
configCmd.AddCommand(configEditCmd)
configCmd.AddCommand(configInitTokenCmd)
configNewCmd.Flags().BoolVar(&flagConfigNewAsEnv, "env", false, "Print configuration as environment variable")
configRegenCmd.Flags().BoolVar(&flagConfigRegenAsEnv, "env", false, "Print configuration as environment variable")
configEditCmd.Flags().BoolVar(&flagConfigEditAsEnv, "env", false, "Print configuration as environment variable")
configEditCmd.Flags().StringVar(&flagConfigEditOutput, "output", "", "output file")
configInitTokenCmd.Flags().StringVar(&flagInitTokenConfigFile, "config", "", "config file")
configInitTokenCmd.Flags().StringVar(&flagInitTokenRemoteConfig, "remote-config", "", "(optional) consul configuration store")
configInitTokenCmd.Flags().StringVar(&flagInitTokenRemoteConfigKey, "remote-config-key", "cds/config.api.toml", "(optional) consul configuration store key")
configInitTokenCmd.Flags().StringVar(&flagInitTokenVaultAddr, "vault-addr", "", "(optional) Vault address to fetch secrets from vault (example: https://vault.mydomain.net:8200)")
configInitTokenCmd.Flags().StringVar(&flagInitTokenVaultToken, "vault-token", "", "(optional) Vault token to fetch secrets from vault")
}
var (
flagConfigNewAsEnv bool
flagConfigRegenAsEnv bool
flagConfigEditAsEnv bool
flagConfigEditOutput string
flagInitTokenConfigFile string
flagInitTokenRemoteConfig string
flagInitTokenRemoteConfigKey string
flagInitTokenVaultAddr string
flagInitTokenVaultToken string
)
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage CDS Configuration",
}
var configNewCmd = &cobra.Command{
Use: "new",
Short: "CDS configuration file assistant",
Long: `
Generate the whole configuration file
$ engine config new > conf.toml
you can compose your file configuration
this will generate a file configuration containing
api and hatchery:local µService
$ engine config new api hatchery:local
For advanced usage, Debug and Tracing section can be generated as:
$ engine config new debug tracing [µService(s)...]
All options
$ engine config new [debug] [tracing] [api] [hatchery:local] [hatchery:openstack] [hatchery:swarm] [hatchery:vsphere] [elasticsearch] [hooks] [vcs] [repositories] [migrate]
`,
Run: func(cmd *cobra.Command, args []string) {
conf := configBootstrap(args)
initToken, err := configSetStartupData(&conf)
if err != nil {
sdk.Exit("%v", err)
}
if !flagConfigNewAsEnv {
btes, err := toml.Marshal(conf)
if err != nil {
sdk.Exit("%v", err)
}
fmt.Println(string(btes))
} else {
configPrintToEnv(conf, os.Stdout)
}
fmt.Println("# On first login, you will be asked to enter the following token:")
fmt.Println("# " + initToken)
},
}
var configCheckCmd = &cobra.Command{
Use: "check",
Short: "Check CDS configuration file",
Long: `$ engine config check <path>`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Help() // nolint
sdk.Exit("Wrong usage")
}
// Initialize config from given path
conf := configImport(nil, args[0], "", "", "", "", false)
var hasError bool
if conf.API != nil && conf.API.URL.API != "" {
fmt.Printf("checking api configuration...\n")
if err := api.New().CheckConfiguration(*conf.API); err != nil {
fmt.Printf("api Configuration: %v\n", err)
hasError = true
}
}
if conf.UI != nil && conf.UI.API.HTTP.URL != "" {
fmt.Printf("checking UI configuration...\n")
if err := ui.New().CheckConfiguration(*conf.UI); err != nil {
fmt.Printf("ui Configuration: %v\n", err)
hasError = true
}
}
if conf.DatabaseMigrate != nil && conf.DatabaseMigrate.API.HTTP.URL != "" {
fmt.Printf("checking migrate configuration...\n")
if err := api.New().CheckConfiguration(*conf.DatabaseMigrate); err != nil {
fmt.Printf("migrate Configuration: %v\n", err)
hasError = true
}
}
if conf.Hatchery != nil && conf.Hatchery.Local != nil && conf.Hatchery.Local.API.HTTP.URL != "" {
fmt.Printf("checking hatchery:local configuration...\n")
if err := local.New().CheckConfiguration(*conf.Hatchery.Local); err != nil {
fmt.Printf("hatchery:local Configuration: %v\n", err)
hasError = true
}
}
if conf.Hatchery != nil && conf.Hatchery.Openstack != nil && conf.Hatchery.Openstack.API.HTTP.URL != "" {
fmt.Printf("checking hatchery:openstack configuration...\n")
if err := openstack.New().CheckConfiguration(*conf.Hatchery.Openstack); err != nil {
fmt.Printf("hatchery:openstack Configuration: %v\n", err)
hasError = true
}
}
if conf.Hatchery != nil && conf.Hatchery.Kubernetes != nil && conf.Hatchery.Kubernetes.API.HTTP.URL != "" {
fmt.Printf("checking hatchery:kubernetes configuration...\n")
if err := kubernetes.New().CheckConfiguration(*conf.Hatchery.Kubernetes); err != nil {
fmt.Printf("hatchery:kubernetes Configuration: %v\n", err)
hasError = true
}
}
if conf.Hatchery != nil && conf.Hatchery.Swarm != nil && conf.Hatchery.Swarm.API.HTTP.URL != "" {
fmt.Printf("checking hatchery:swarm configuration...\n")
if err := swarm.New().CheckConfiguration(*conf.Hatchery.Swarm); err != nil {
fmt.Printf("hatchery:swarm Configuration: %v\n", err)
hasError = true
}
}
if conf.Hatchery != nil && conf.Hatchery.VSphere != nil && conf.Hatchery.VSphere.API.HTTP.URL != "" {
fmt.Printf("checking hatchery:vsphere configuration...\n")
if err := vsphere.New().CheckConfiguration(*conf.Hatchery.VSphere); err != nil {
fmt.Printf("hatchery:vsphere Configuration: %v\n", err)
hasError = true
}
}
if conf.VCS != nil && conf.VCS.API.HTTP.URL != "" {
fmt.Printf("checking vcs configuration...\n")
if err := vcs.New().CheckConfiguration(*conf.VCS); err != nil {
fmt.Printf("vcs Configuration: %v\n", err)
hasError = true
}
}
if conf.Hooks != nil && conf.Hooks.API.HTTP.URL != "" {
fmt.Printf("checking hooks configuration...\n")
if err := hooks.New().CheckConfiguration(*conf.Hooks); err != nil {
fmt.Printf("hooks Configuration: %v\n", err)
hasError = true
}
}
if conf.CDN != nil && conf.CDN.API.HTTP.URL != "" {
fmt.Printf("checking cdn configuration...\n")
if err := cdn.New().CheckConfiguration(*conf.CDN); err != nil {
fmt.Printf("cdn Configuration: %v\n", err)
hasError = true
}
}
if !hasError {
fmt.Println("Configuration file OK")
}
},
}
var configRegenCmd = &cobra.Command{
Use: "regen",
Short: "Regen tokens and keys for given CDS configuration file",
Long: `$ engine config regen <input-path> <output-path>`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
cmd.Help() // nolint
sdk.Exit("Wrong usage")
}
oldConf := configImport(nil, args[0], "", "", "", "", true)
initToken, err := configSetStartupData(&oldConf)
if err != nil {
sdk.Exit("%v", err)
}
writer := os.Stdout
if len(args) == 2 {
output := args[1]
if _, err := os.Stat(output); err == nil {
if err := os.Remove(output); err != nil {
sdk.Exit("%v", err)
}
}
writer, err = os.Create(output)
if err != nil {
sdk.Exit("%v", err)
}
}
defer writer.Close()
if !flagConfigRegenAsEnv {
btes, err := toml.Marshal(oldConf)
if err != nil {
sdk.Exit("%v", err)
}
fmt.Fprintln(writer, string(btes))
} else {
configPrintToEnv(oldConf, writer)
}
fmt.Fprintln(writer, "# On first login, you will be asked to enter the following token:")
fmt.Fprintln(writer, "# "+initToken)
},
}
var configEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit a CDS configuration file",
Long: `$ engine config edit <path-toml-file> key=value key=value`,
Example: `$ engine config edit conf.toml log.level=debug hatchery.swarm.commonConfiguration.name=hatchery-swarm-name`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
cmd.Help() // nolint
sdk.Exit("Wrong usage")
}
cfgFile := args[0]
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
sdk.Exit("File %s doesn't exist", cfgFile)
}
btes, err := os.ReadFile(cfgFile)
if err != nil {
sdk.Exit("Error while read content of file %s - err:%v", cfgFile, err)
}
tomlConf, err := toml.Load(string(btes))
if err != nil {
sdk.Exit("Error while load toml content of file %s - err:%v", cfgFile, err)
}
for _, vk := range args[1:] {
t := strings.SplitN(vk, "=", 2)
if len(t) != 2 {
sdk.Exit("Invalid key=value: %v", vk)
}
// check if value is bool, float, int or else string
if v, err := strconv.ParseBool(t[1]); err == nil {
tomlConf.Set(t[0], "", false, "", v)
} else if v, err := strconv.ParseInt(t[1], 10, 64); err == nil {
tomlConf.Set(t[0], "", false, "", v)
} else {
tomlConf.Set(t[0], "", false, "", t[1])
}
}
tmpFile := "cds.tmp.toml"
if err := os.WriteFile(tmpFile, []byte(tomlConf.String()), os.FileMode(0640)); err != nil {
sdk.Exit("Error while create tempfile: %v", err)
}
defer os.Remove(tmpFile)
conf := configImport(nil, tmpFile, "", "", "", "", true)
writer := os.Stdout
if flagConfigEditOutput != "" {
if _, err := os.Stat(flagConfigEditOutput); err == nil {
if err := os.Remove(flagConfigEditOutput); err != nil {
sdk.Exit("%v", err)
}
}
writer, err = os.Create(flagConfigEditOutput)
if err != nil {
sdk.Exit("%v", err)
}
}
defer writer.Close()
if !flagConfigEditAsEnv {
btes, err := toml.Marshal(conf)
if err != nil {
sdk.Exit("%v", err)
}
fmt.Fprintln(writer, string(btes))
} else {
configPrintToEnv(conf, writer)
}
},
}
var configInitTokenCmd = &cobra.Command{
Use: "init-token",
Short: "Generate/Regenerate an init token for a given CDS configuration file",
Long: `$ engine config init-token`,
Run: func(cmd *cobra.Command, args []string) {
conf := configImport(args, flagInitTokenConfigFile, flagInitTokenRemoteConfig, flagInitTokenRemoteConfigKey, flagInitTokenVaultAddr, flagInitTokenVaultToken, true)
initToken, err := getInitTokenFromExistingConfiguration(conf)
if err != nil {
sdk.Exit("error: %v", err)
}
fmt.Println(initToken)
},
}