-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.go
595 lines (521 loc) · 25.4 KB
/
server.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// 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.
package commands
import (
"fmt"
"os"
"strings"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ystia/yorc/config"
"github.com/ystia/yorc/helper/collections"
"github.com/ystia/yorc/log"
"github.com/ystia/yorc/server"
)
func init() {
RootCmd.AddCommand(serverCmd)
// Get the CLI args
args := os.Args
serverInitExtraFlags(args)
setConfig()
}
const (
// EnvironmentVariablePrefix is the prefix used in Yorc commands parameters
// passed as environment variables
EnvironmentVariablePrefix = "YORC"
)
var (
// TfConsulPluginVersion is the Terraform Consul plugin lowest supported version
TfConsulPluginVersion = "tf Consul plugin version"
tfConsulPluginVersionConstraint = versionToConstraint("~>", TfConsulPluginVersion, "minor")
// TfAWSPluginVersion is the Terraform AWS plugin lowest supported version
TfAWSPluginVersion = "tf AWS plugin version"
tfAWSPluginVersionConstraint = versionToConstraint("~>", TfAWSPluginVersion, "minor")
// TfOpenStackPluginVersion is the Terraform OpenStack plugin lowest supported version
TfOpenStackPluginVersion = "tf OpenStack plugin version"
tfOpenStackPluginVersionConstraint = versionToConstraint("~>", TfOpenStackPluginVersion, "minor")
// TfGooglePluginVersion is the Terraform Google plugin lowest supported version
TfGooglePluginVersion = "tf Google plugin version"
tfGooglePluginVersionConstraint = versionToConstraint("~>", TfGooglePluginVersion, "minor")
)
var ansibleConfiguration = map[string]interface{}{
"ansible.use_openssh": false,
"ansible.debug": false,
"ansible.connection_retries": 5,
"ansible.operation_remote_base_dir": ".yorc",
"ansible.keep_operation_remote_path": config.DefaultKeepOperationRemotePath,
"ansible.archive_artifacts": config.DefaultArchiveArtifacts,
"ansible.cache_facts": config.DefaultCacheFacts,
"ansible.keep_generated_recipes": false,
"ansible.job_monitoring_time_interval": config.DefaultAnsibleJobMonInterval,
}
var consulConfiguration = map[string]interface{}{
"consul.address": "",
"consul.token": "anonymous",
"consul.datacenter": "dc1",
"consul.key_file": "",
"consul.cert_file": "",
"consul.ca_cert": "",
"consul.ca_path": "",
"consul.ssl": false,
"consul.ssl_verify": true,
"consul.publisher_max_routines": config.DefaultConsulPubMaxRoutines,
"consul.tls_handshake_timeout": config.DefaultConsulTLSHandshakeTimeout,
}
var terraformConfiguration = map[string]interface{}{
"terraform.plugins_dir": "",
"terraform.consul_plugin_version_constraint": tfConsulPluginVersionConstraint,
"terraform.aws_plugin_version_constraint": tfAWSPluginVersionConstraint,
"terraform.google_plugin_version_constraint": tfGooglePluginVersionConstraint,
"terraform.openstack_plugin_version_constraint": tfOpenStackPluginVersionConstraint,
"terraform.keep_generated_files": false,
}
var cfgFile string
var resolvedServerExtraParams []*serverExtraParams
type serverExtraParams struct {
argPrefix string
envPrefix string
viperPrefix string
viperNames []string
subSplit int
storeFn serverExtraParamStoreFn
readConfFn serverExtraParamReadConf
}
type serverExtraParamStoreFn func(cfg *config.Configuration, param string)
type serverExtraParamReadConf func(cfg *config.Configuration)
var serverCmd = &cobra.Command{
Use: "server",
Short: "Perform the server command",
Long: `Perform the server command`,
SilenceUsage: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initConfig()
},
RunE: func(cmd *cobra.Command, args []string) error {
log.Println("Using config file:", viper.ConfigFileUsed())
shutdownCh := make(chan struct{})
return RunServer(shutdownCh)
},
}
// RunServer starts a Yorc Server
func RunServer(shutdownCh chan struct{}) error {
configuration := GetConfig()
log.Debugf("Configuration :%+v", configuration)
return server.RunServer(configuration, shutdownCh)
}
func serverInitExtraFlags(args []string) {
InitExtraFlags(args, serverCmd)
}
// InitExtraFlags inits infrastructure and vault flags
func InitExtraFlags(args []string, cmd *cobra.Command) {
resolvedServerExtraParams = []*serverExtraParams{
&serverExtraParams{
argPrefix: "infrastructure_",
envPrefix: "YORC_INFRA_",
viperPrefix: "infrastructures.",
viperNames: make([]string, 0),
subSplit: 1,
storeFn: addServerExtraInfraParams,
readConfFn: readInfraViperConfig,
},
&serverExtraParams{
argPrefix: "vault_",
envPrefix: "YORC_VAULT_",
viperPrefix: "vault.",
viperNames: make([]string, 0),
storeFn: addServerExtraVaultParam,
readConfFn: readVaultViperConfig,
},
}
for _, sep := range resolvedServerExtraParams {
for i := range args {
if strings.HasPrefix(args[i], "--"+sep.argPrefix) {
var viperName, flagName string
if strings.ContainsRune(args[i], '=') {
// Handle the syntax --infrastructure_xxx_yyy = value
flagParts := strings.Split(args[i], "=")
flagName = strings.TrimLeft(flagParts[0], "-")
viperName = strings.Replace(strings.Replace(flagName, sep.argPrefix, sep.viperPrefix, 1), "_", ".", sep.subSplit)
if len(flagParts) == 1 {
// Boolean flag
cmd.PersistentFlags().Bool(flagName, false, "")
viper.SetDefault(viperName, false)
} else {
cmd.PersistentFlags().String(flagName, "", "")
viper.SetDefault(viperName, "")
}
} else {
// Handle the syntax --infrastructure_xxx_yyy value
flagName = strings.TrimLeft(args[i], "-")
viperName = strings.Replace(strings.Replace(flagName, sep.argPrefix, sep.viperPrefix, 1), "_", ".", sep.subSplit)
if len(args) > i+1 && !strings.HasPrefix(args[i+1], "--") {
// Arguments ending wih a plural 's' are considered to
// be slices
if strings.HasSuffix(args[i], "s") && !strings.HasSuffix(args[i], "credentials") {
// May have already been defined as string slice
// flags can appear several times
if cmd.PersistentFlags().Lookup(flagName) == nil {
cmd.PersistentFlags().StringSlice(flagName, []string{}, "")
viper.SetDefault(viperName, []string{})
}
} else {
cmd.PersistentFlags().String(flagName, "", "")
viper.SetDefault(viperName, "")
}
} else {
// Boolean flag
cmd.PersistentFlags().Bool(flagName, false, "")
viper.SetDefault(viperName, false)
}
}
// Add viper flag
viper.BindPFlag(viperName, cmd.PersistentFlags().Lookup(flagName))
sep.viperNames = append(sep.viperNames, viperName)
}
}
for _, envVar := range os.Environ() {
if strings.HasPrefix(envVar, sep.envPrefix) {
envVarParts := strings.SplitN(envVar, "=", 2)
viperName := strings.ToLower(strings.Replace(strings.Replace(envVarParts[0], sep.envPrefix, sep.viperPrefix, 1), "_", ".", sep.subSplit))
viper.BindEnv(viperName, envVarParts[0])
if !collections.ContainsString(sep.viperNames, viperName) {
sep.viperNames = append(sep.viperNames, viperName)
}
}
}
}
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// enable ability to specify config file via flag
viper.SetConfigFile(cfgFile)
}
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err != nil {
log.Println("Can't use config file:", err)
} else {
// Watch config to take into account config changes
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Printf("Reloading config on config file %s change\n", e.Name)
viper.ReadInConfig()
})
}
// Deprecate Ansible and Consul flat keys if they are defined in
// configuration
deprecateFlatKeys(ansibleConfiguration, "ansible")
deprecateFlatKeys(consulConfiguration, "consul")
}
func setConfig() {
host, err := os.Hostname()
if err != nil {
host = "server_0"
log.Printf("Failed to get system hostname: %v. We will try to use the default id (%q) for this instance.", err, host)
}
//Flags definition for Yorc server
serverCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is /etc/yorc/config.yorc.json)")
serverCmd.PersistentFlags().String("plugins_directory", config.DefaultPluginDir, "The name of the plugins directory of the Yorc server")
serverCmd.PersistentFlags().StringP("working_directory", "w", "", "The name of the working directory of the Yorc server")
serverCmd.PersistentFlags().Int("workers_number", config.DefaultWorkersNumber, "Number of workers in the Yorc server. If not set the default value will be used")
serverCmd.PersistentFlags().Duration("graceful_shutdown_timeout", config.DefaultServerGracefulShutdownTimeout, "Timeout to wait for a graceful shutdown of the Yorc server. After this delay the server immediately exits.")
serverCmd.PersistentFlags().StringP("resources_prefix", "x", "", "Prefix created resources (like Computes and so on)")
serverCmd.PersistentFlags().Duration("wf_step_graceful_termination_timeout", config.DefaultWfStepGracefulTerminationTimeout, "Timeout to wait for a graceful termination of a workflow step during concurrent workflow step failure. After this delay the step is set on error.")
serverCmd.PersistentFlags().String("server_id", host, "The server ID used to identify the server node in a cluster.")
serverCmd.PersistentFlags().Bool("disable_ssh_agent", false, "Allow disabling ssh-agent use for SSH authentication on provisioned computes. Default is false. If true, compute credentials must provide a path to a private key file instead of key content.")
// Flags definition for Yorc HTTP REST API
serverCmd.PersistentFlags().Int("http_port", config.DefaultHTTPPort, "Port number for the Yorc HTTP REST API. If omitted or set to '0' then the default port number is used, any positive integer will be used as it, and finally any negative value will let use a random port.")
serverCmd.PersistentFlags().String("http_address", config.DefaultHTTPAddress, "Listening address for the Yorc HTTP REST API.")
serverCmd.PersistentFlags().String("key_file", "", "File path to a PEM-encoded private key. The key is used to enable SSL for the Yorc HTTP REST API. This must be provided along with cert_file. If one of key_file or cert_file is not provided then SSL is disabled.")
serverCmd.PersistentFlags().String("ca_file", "", "File path to a PEM-encoded CA certificate to use for talking to yorc over TLS")
serverCmd.PersistentFlags().String("ca_path", "", "Path to a directory of CA certs to use for talking to yorc over TLS")
serverCmd.PersistentFlags().String("cert_file", "", "File path to a PEM-encoded certificate. The certificate is used to enable SSL for the Yorc HTTP REST API. This must be provided along with key_file. If one of key_file or cert_file is not provided then SSL is disabled.")
serverCmd.PersistentFlags().Bool("ssl_verify", false, "Whether or not enable client certificate checking by the server")
//Flags definition for Consul
serverCmd.PersistentFlags().StringP("consul_address", "", "", "Address of the HTTP interface for Consul (format: <host>:<port>)")
serverCmd.PersistentFlags().StringP("consul_token", "t", "", "The token by default")
serverCmd.PersistentFlags().StringP("consul_datacenter", "d", "", "The datacenter of Consul node")
serverCmd.PersistentFlags().String("consul_key_file", "", "The key file to use for talking to Consul over TLS")
serverCmd.PersistentFlags().String("consul_cert_file", "", "The cert file to use for talking to Consul over TLS")
serverCmd.PersistentFlags().String("consul_ca_cert", "", "CA cert to use for talking to Consul over TLS")
serverCmd.PersistentFlags().String("consul_ca_path", "", "Path to a directory of CA certs to use for talking to Consul over TLS")
serverCmd.PersistentFlags().Bool("consul_ssl", false, "Whether or not to use HTTPS")
serverCmd.PersistentFlags().Bool("consul_ssl_verify", true, "Whether or not to disable certificate checking")
serverCmd.PersistentFlags().Int("consul_publisher_max_routines", config.DefaultConsulPubMaxRoutines, "Maximum number of parallelism used to store TOSCA definitions in Consul. If you increase the default value you may need to tweak the ulimit max open files. If set to 0 or less the default value will be used")
serverCmd.PersistentFlags().Duration("consul_tls_handshake_timeout", config.DefaultConsulTLSHandshakeTimeout, "Maximum duration to wait for a TLS handshake with Consul")
serverCmd.PersistentFlags().Bool("ansible_use_openssh", false, "Prefer OpenSSH over Paramiko a Python implementation of SSH (the default) to provision remote hosts")
serverCmd.PersistentFlags().Bool("ansible_debug", false, "Prints massive debug information from Ansible")
serverCmd.PersistentFlags().Int("ansible_connection_retries", 5, "Number of retries in case of Ansible SSH connection failure")
serverCmd.PersistentFlags().String("operation_remote_base_dir", ".yorc", "Name of the temporary directory used by Ansible on the nodes")
serverCmd.PersistentFlags().Bool("keep_operation_remote_path", config.DefaultKeepOperationRemotePath, "Define wether the path created to store artifacts on the nodes will be removed at the end of workflow executions.")
serverCmd.PersistentFlags().Bool("ansible_archive_artifacts", config.DefaultArchiveArtifacts, "Define wether artifacts should be ./archived before being copied on remote nodes (requires tar to be installed on remote nodes).")
serverCmd.PersistentFlags().Bool("ansible_cache_facts", config.DefaultCacheFacts, "Define wether Ansible facts (useful variables about remote hosts) should be cached.")
serverCmd.PersistentFlags().Bool("ansible_keep_generated_recipes", false, "Define if Yorc should not delete generated Ansible recipes")
serverCmd.PersistentFlags().Duration("ansible_job_monitoring_time_interval", config.DefaultAnsibleJobMonInterval, "Default duration for monitoring time interval for jobs handled by Ansible")
//Flags definition for Terraform
serverCmd.PersistentFlags().Bool("terraform_keep_generated_files", false, "Define if Yorc should not delete generated Terraform infrastructures files")
//Flags definition for Terraform
serverCmd.PersistentFlags().StringP("terraform_plugins_dir", "", "", "The directory where to find Terraform plugins")
serverCmd.PersistentFlags().StringP("terraform_consul_plugin_version_constraint", "", tfConsulPluginVersionConstraint, "Terraform Consul plugin version constraint.")
serverCmd.PersistentFlags().StringP("terraform_aws_plugin_version_constraint", "", tfAWSPluginVersionConstraint, "Terraform AWS plugin version constraint.")
serverCmd.PersistentFlags().StringP("terraform_openstack_plugin_version_constraint", "", tfOpenStackPluginVersionConstraint, "Terraform OpenStack plugin version constraint.")
serverCmd.PersistentFlags().StringP("terraform_google_plugin_version_constraint", "", tfGooglePluginVersionConstraint, "Terraform Google plugin version constraint.")
//Bind Consul persistent flags
for key := range consulConfiguration {
viper.BindPFlag(key, serverCmd.PersistentFlags().Lookup(toFlatKey(key)))
}
//Bind Flags for Yorc server
viper.BindPFlag("working_directory", serverCmd.PersistentFlags().Lookup("working_directory"))
viper.BindPFlag("plugins_directory", serverCmd.PersistentFlags().Lookup("plugins_directory"))
viper.BindPFlag("workers_number", serverCmd.PersistentFlags().Lookup("workers_number"))
viper.BindPFlag("server_graceful_shutdown_timeout", serverCmd.PersistentFlags().Lookup("graceful_shutdown_timeout"))
viper.BindPFlag("resources_prefix", serverCmd.PersistentFlags().Lookup("resources_prefix"))
viper.BindPFlag("wf_step_graceful_termination_timeout", serverCmd.PersistentFlags().Lookup("wf_step_graceful_termination_timeout"))
viper.BindPFlag("server_id", serverCmd.PersistentFlags().Lookup("server_id"))
viper.BindPFlag("disable_ssh_agent", serverCmd.PersistentFlags().Lookup("disable_ssh_agent"))
//Bind Flags Yorc HTTP REST API
viper.BindPFlag("http_port", serverCmd.PersistentFlags().Lookup("http_port"))
viper.BindPFlag("http_address", serverCmd.PersistentFlags().Lookup("http_address"))
viper.BindPFlag("cert_file", serverCmd.PersistentFlags().Lookup("cert_file"))
viper.BindPFlag("key_file", serverCmd.PersistentFlags().Lookup("key_file"))
viper.BindPFlag("ca_file", serverCmd.PersistentFlags().Lookup("ca_file"))
viper.BindPFlag("ca_path", serverCmd.PersistentFlags().Lookup("ca_path"))
viper.BindPFlag("ssl_verify", serverCmd.PersistentFlags().Lookup("ssl_verify"))
//Bind Ansible persistent flags
for key := range ansibleConfiguration {
viper.BindPFlag(key, serverCmd.PersistentFlags().Lookup(toFlatKey(key)))
}
//Bind Terraform persistent flags
for key := range terraformConfiguration {
viper.BindPFlag(key, serverCmd.PersistentFlags().Lookup(toFlatKey(key)))
}
//Environment Variables
viper.SetEnvPrefix(EnvironmentVariablePrefix)
viper.AutomaticEnv() // read in environment variables that match
viper.BindEnv("working_directory")
viper.BindEnv("plugins_directory")
viper.BindEnv("server_graceful_shutdown_timeout")
viper.BindEnv("workers_number")
viper.BindEnv("http_port")
viper.BindEnv("http_address")
viper.BindEnv("key_file")
viper.BindEnv("ca_file")
viper.BindEnv("ca_path")
viper.BindEnv("cert_file")
viper.BindEnv("SSL_verify")
viper.BindEnv("resources_prefix")
viper.BindEnv("server_id")
viper.BindEnv("disable_ssh_agent")
//Bind Consul environment variables flags
for key := range consulConfiguration {
viper.BindEnv(key, toEnvVar(key))
}
viper.BindEnv("wf_step_graceful_termination_timeout")
//Bind Ansible environment variables flags
for key := range ansibleConfiguration {
viper.BindEnv(key, toEnvVar(key))
}
//Bind Terraform environment variables flags
for key := range terraformConfiguration {
viper.BindEnv(key, toEnvVar(key))
}
//Setting Defaults
viper.SetDefault("working_directory", "work")
viper.SetDefault("server_graceful_shutdown_timeout", config.DefaultServerGracefulShutdownTimeout)
viper.SetDefault("plugins_directory", config.DefaultPluginDir)
viper.SetDefault("http_port", config.DefaultHTTPPort)
viper.SetDefault("http_address", config.DefaultHTTPAddress)
viper.SetDefault("resources_prefix", "yorc-")
viper.SetDefault("workers_number", config.DefaultWorkersNumber)
viper.SetDefault("wf_step_graceful_termination_timeout", config.DefaultWfStepGracefulTerminationTimeout)
viper.SetDefault("server_id", host)
viper.SetDefault("disable_ssh_agent", false)
// Consul configuration default settings
for key, value := range consulConfiguration {
viper.SetDefault(key, value)
}
// Ansible configuration default settings
for key, value := range ansibleConfiguration {
viper.SetDefault(key, value)
}
// Terraform configuration default settings
for key, value := range terraformConfiguration {
viper.SetDefault(key, value)
}
//Configuration file directories
viper.SetConfigName("config.yorc") // name of config file (without extension)
viper.AddConfigPath("/etc/yorc/") // adding home directory as first search path
viper.AddConfigPath(".")
}
// GetConfig gets configuration from viper
func GetConfig() config.Configuration {
configuration := config.Configuration{}
err := viper.Unmarshal(&configuration)
if err != nil {
log.Fatalf("Misconfiguration error: %v", err)
}
if configuration.Infrastructures == nil {
configuration.Infrastructures = make(map[string]config.DynamicMap)
}
if configuration.Vault == nil {
configuration.Vault = make(config.DynamicMap)
}
for _, sep := range resolvedServerExtraParams {
sep.readConfFn(&configuration)
for _, infraParam := range sep.viperNames {
sep.storeFn(&configuration, infraParam)
}
}
return configuration
}
func readInfraViperConfig(cfg *config.Configuration) {
infras := viper.GetStringMap("infrastructures")
for infraName, infraConf := range infras {
infraConfMap, ok := infraConf.(map[string]interface{})
if !ok {
tmpInfraMap, ok := infraConf.(map[interface{}]interface{})
if !ok {
log.Fatalf("Invalid configuration format for infrastructure %q", infraName)
}
infraConfMap = make(map[string]interface{})
for k, v := range tmpInfraMap {
infraConfMap[fmt.Sprint(k)] = v
}
}
if cfg.Infrastructures[infraName] == nil {
cfg.Infrastructures[infraName] = make(config.DynamicMap)
}
for k, v := range infraConfMap {
cfg.Infrastructures[infraName].Set(k, v)
}
}
}
func readVaultViperConfig(cfg *config.Configuration) {
vaultCfg := viper.GetStringMap("vault")
for k, v := range vaultCfg {
cfg.Vault.Set(k, v)
}
}
func addServerExtraInfraParams(cfg *config.Configuration, infraParam string) {
if cfg.Infrastructures == nil {
cfg.Infrastructures = make(map[string]config.DynamicMap)
}
paramParts := strings.Split(infraParam, ".")
value := viper.Get(infraParam)
params, ok := cfg.Infrastructures[paramParts[1]]
if !ok {
params = make(config.DynamicMap)
cfg.Infrastructures[paramParts[1]] = params
}
// When the key/value pair is read from an environment variable, the value is
// read as a string. This needs to be changed if the variable is expected to
// be an array
if strings.HasSuffix(paramParts[2], "s") && !strings.HasSuffix(paramParts[2], "credentials") {
// value should be a slice
switch value.(type) {
case string:
vSlice := strings.Split(fmt.Sprint(value), ",")
for i, val := range vSlice {
vSlice[i] = strings.TrimSpace(val)
}
params.Set(paramParts[2], vSlice)
default:
params.Set(paramParts[2], value)
}
} else {
params.Set(paramParts[2], value)
}
}
func addServerExtraVaultParam(cfg *config.Configuration, vaultParam string) {
paramParts := strings.Split(vaultParam, ".")
value := viper.Get(vaultParam)
cfg.Vault.Set(paramParts[1], value)
}
// Deprecate keys still using an old format in viper configuration by defining
// an alias to the new key, and logging a message describing which keys are
// deprecated as well as the new format to use.
func deprecateFlatKeys(configuration map[string]interface{}, configurationName string) {
var deprecatedMsg string
var newValueMsg string
msgFlatKeyFormat := "\t%q: %T,\n"
msgNestedKeyFormat := "\t" + msgFlatKeyFormat
for key, defaultValue := range configuration {
deprecatedKey := toFlatKey(key)
if value := viper.Get(deprecatedKey); value != nil {
subkeys := strings.SplitN(key, ".", 2)
deprecatedMsg += fmt.Sprintf(msgFlatKeyFormat, deprecatedKey, defaultValue)
newValueMsg += fmt.Sprintf(msgNestedKeyFormat, subkeys[1], defaultValue)
// Let viper manage the nested key as the primary key,
// and the flat key as an alias
viper.RegisterAlias(deprecatedKey, key)
}
}
if deprecatedMsg != "" {
log.Printf("Deprecated values are used in configuration file. The following lines:\n%sshould now have this format:\n\t%q:{\n%s\t}",
deprecatedMsg,
configurationName,
newValueMsg)
}
}
// Returns the flat key corresponding to a nested key.
// For example, for a nested key consul.token, this function will return
// consul_token
func toFlatKey(nestedKey string) string {
var flatKey string
// Specific code for keys that don't follow the naming scheme
if nestedKey == "ansible.operation_remote_base_dir" ||
nestedKey == "ansible.keep_operation_remote_path" {
flatKey = strings.Replace(nestedKey, "ansible.", "", 1)
} else {
flatKey = strings.Replace(nestedKey, ".", "_", 1)
}
return flatKey
}
// Returns the name of the environment variable corresponding to a viper
// nested key. For example, using the prefix yorc,
// nested key consul.token will be associated to YORC_CONSUL_TOKEN environment
// variable
func toEnvVar(key string) string {
name := EnvironmentVariablePrefix + "_" + toFlatKey(key)
return strings.ToUpper(name)
}
// For a defined constraint symbol, a defined version as major.minor.patch ans a defined constraint level, it returns the version constraint
func versionToConstraint(constraint, version, level string) string {
switch strings.ToLower(level) {
case "major":
// returns the constraint + major digit a
tab := strings.Split(version, ".")
if len(tab) > 1 {
return fmt.Sprintf("%s %s", strings.TrimSpace(constraint), tab[0])
}
return fmt.Sprintf("%s %s", strings.TrimSpace(constraint), version)
case "minor":
// returns the constraint + major and minor digits a.b
tab := strings.Split(version, ".")
if len(tab) > 2 {
return fmt.Sprintf("%s %s.%s", strings.TrimSpace(constraint), tab[0], tab[1])
}
return fmt.Sprintf("%s %s", strings.TrimSpace(constraint), version)
case "patch":
// returns the constraint + major,minor and patch digits a.b.c
tab := strings.Split(version, ".")
if len(tab) > 3 {
return fmt.Sprintf("%s %s.%s.%s", strings.TrimSpace(constraint), tab[0], tab[1], tab[2])
}
return fmt.Sprintf("%s %s", strings.TrimSpace(constraint), version)
default:
panic("unable to build related constraint for constraint:%q, version:%q and level:%q. Unknown level")
}
}