Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read config values from env on init command #663

Merged
merged 21 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ed30ac1
Add ApplyEnvVars() to ComponentConfig interface
roignpar Feb 7, 2019
032f028
Implement ApplyEnvVars for all ComponentConfigs
roignpar Feb 8, 2019
21a5391
Remove json config overriding with env vars
roignpar Feb 8, 2019
da58eae
Add ipfsproxy NodeHTTPS config field to ToJSON
roignpar Feb 11, 2019
15ac8cd
Use ParseDurations when applying json to cluster Config
roignpar Feb 11, 2019
580020d
Use separate envConfigKeys for metrics and tracing
roignpar Feb 13, 2019
aa5d545
Implement ApplyEnvVars for ipfshttp Config
roignpar Feb 13, 2019
47252f8
Merge remote-tracking branch 'upstream/master' into issue_656
roignpar Feb 15, 2019
168cf76
Change ApplyEnvVars strategy for all config components
roignpar Feb 15, 2019
523e109
Create LoadJSONFileAndEnv config method for convenience
roignpar Feb 15, 2019
78ac49a
Fix env config tests
roignpar Feb 18, 2019
36ee0f8
Add ApplyEnvVars test to ipfsproxy config
roignpar Feb 18, 2019
bac982c
Add ApplyEnvVars test to raft config
roignpar Feb 18, 2019
08580c3
Add ApplyEnvVars test to disk config
roignpar Feb 18, 2019
e38ceab
Add ApplyEnvVars test to numpin config
roignpar Feb 18, 2019
be8c56f
Add ApplyEnvVars test to ipfshttp config
roignpar Feb 18, 2019
40d1077
Add ApplyEnvVars test to monbasic config
roignpar Feb 18, 2019
38886da
Add ApplyEnvVars test to pubsubmon config
roignpar Feb 18, 2019
368f1de
Add ApplyEnvVars test to maptracker config
roignpar Feb 18, 2019
50844b9
Add ApplyEnvVars test to stateless config
roignpar Feb 18, 2019
06482e5
Add ApplyEnvVars test to observations config
roignpar Feb 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/ipfsproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have sensible values,
// at least in appearance.
func (cfg *Config) Validate() error {
Expand Down
19 changes: 18 additions & 1 deletion api/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
jcfg := &jsonConfig{}

err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}

return cfg.applyJSONConfig(jcfg)
}

// Validate makes sure that all fields in this Config have
// working values, at least in appearance.
func (cfg *Config) Validate() error {
Expand Down Expand Up @@ -236,7 +249,11 @@ func (cfg *Config) LoadJSON(raw []byte) error {
return err
}

err = cfg.loadHTTPOptions(jcfg)
return cfg.applyJSONConfig(jcfg)
}

func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
err := cfg.loadHTTPOptions(jcfg)
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
jcfg := &configJSON{}

err := envconfig.Process(cfg.ConfigKey(), jcfg)
hsanjuan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

return cfg.applyConfigJSON(jcfg)
}

// Validate will check that the values of this config
// seem to be working ones.
func (cfg *Config) Validate() error {
Expand Down Expand Up @@ -311,6 +324,10 @@ for more information.`)
return err
}

return cfg.applyConfigJSON(jcfg)
}

func (cfg *Config) applyConfigJSON(jcfg *configJSON) error {
parseDuration := func(txt string) time.Duration {
d, _ := time.ParseDuration(txt)
if txt != "" && d == 0 {
Expand Down
17 changes: 8 additions & 9 deletions cmd/ipfs-cluster-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ configuration.
err := cfgMgr.Default()
checkErr("generating default configuration", err)

err = cfgMgr.ApplyEnvVars()
checkErr("applying environment variables to configuration", err)

// Set user secret
if userSecretDefined {
cfgs.clusterCfg.Secret = userSecret
Expand Down Expand Up @@ -501,18 +504,14 @@ func setupDebug() {
}

func userProvidedSecret(enterSecret bool) ([]byte, bool) {
var secret string
if enterSecret {
secret = promptUser("Enter cluster secret (32-byte hex string): ")
} else if envSecret, envSecretDefined := os.LookupEnv("CLUSTER_SECRET"); envSecretDefined {
secret = envSecret
} else {
return nil, false
secret := promptUser("Enter cluster secret (32-byte hex string): ")
decodedSecret, err := ipfscluster.DecodeClusterSecret(secret)
checkErr("parsing user-provided secret", err)
return decodedSecret, true
}

decodedSecret, err := ipfscluster.DecodeClusterSecret(secret)
checkErr("parsing user-provided secret", err)
return decodedSecret, true
return nil, false
}

func promptUser(msg string) string {
Expand Down
26 changes: 26 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type ComponentConfig interface {
ToJSON() ([]byte, error)
// Sets default working values
Default() error
// Sets values from environment variables
ApplyEnvVars() error
// Allows this component to work under a subfolder
SetBaseDir(string)
// Checks that the configuration is valid
Expand Down Expand Up @@ -225,6 +227,30 @@ func (cfg *Manager) Default() error {
return nil
}

// ApplyEnvVars overrides configuration fields with any values found
// in environment variables.
func (cfg *Manager) ApplyEnvVars() error {
for _, section := range cfg.sections {
for k, compcfg := range section {
logger.Debugf("applying environment variables conf for %s", k)
err := compcfg.ApplyEnvVars()
if err != nil {
return err
}
}
}

if cfg.clusterConfig != nil {
logger.Debugf("applying environment variables conf for cluster")
err := cfg.clusterConfig.ApplyEnvVars()
if err != nil {
return err
}
}

return nil
}

// RegisterComponent lets the Manager load and save component configurations
func (cfg *Manager) RegisterComponent(t SectionType, ccfg ComponentConfig) {
cfg.wg.Add(1)
Expand Down
7 changes: 7 additions & 0 deletions consensus/raft/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// GetDataFolder returns the Raft data folder that we are using.
func (cfg *Config) GetDataFolder() string {
if cfg.DataFolder == "" {
Expand Down
7 changes: 7 additions & 0 deletions informer/disk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *Config) Validate() error {
Expand Down
7 changes: 7 additions & 0 deletions informer/numpin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this configuration have
// sensible values.
func (cfg *Config) Validate() error {
Expand Down
7 changes: 7 additions & 0 deletions ipfsconn/ipfshttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have sensible values,
// at least in appearance.
func (cfg *Config) Validate() error {
Expand Down
7 changes: 7 additions & 0 deletions monitor/basic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *Config) Validate() error {
Expand Down
7 changes: 7 additions & 0 deletions monitor/pubsubmon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *Config) Validate() error {
Expand Down
14 changes: 14 additions & 0 deletions observations/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func (cfg *MetricsConfig) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *MetricsConfig) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *MetricsConfig) Validate() error {
Expand Down Expand Up @@ -159,6 +166,13 @@ func (cfg *TracingConfig) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *TracingConfig) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *TracingConfig) Validate() error {
Expand Down
7 changes: 7 additions & 0 deletions pintracker/maptracker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *Config) Validate() error {
Expand Down
7 changes: 7 additions & 0 deletions pintracker/stateless/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ func (cfg *Config) Default() error {
return nil
}

// ApplyEnvVars fills in any Config fields found
// as environment variables.
func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env
return nil
}

// Validate checks that the fields of this Config have working values,
// at least in appearance.
func (cfg *Config) Validate() error {
Expand Down