From a63abf8fab64f36150e8f2611b1e3cf81bae4c34 Mon Sep 17 00:00:00 2001 From: Kaloyan Raev Date: Tue, 15 Jan 2019 15:55:33 +0200 Subject: [PATCH] Save only user-specific flags to storage node's config.yaml (#1051) --- cmd/bootstrap/main.go | 2 +- cmd/captplanet/setup.go | 2 +- cmd/certificates/setup.go | 2 +- cmd/satellite/main.go | 2 +- cmd/storagenode/main.go | 13 +++++++++---- cmd/uplink/cmd/setup.go | 2 +- pkg/cfgstruct/bind.go | 11 +++++++---- pkg/kademlia/config.go | 6 +++--- pkg/piecestore/psserver/config.go | 6 +++--- pkg/process/exec_conf.go | 29 ++++++++++++++++++++++++----- pkg/server/config.go | 2 +- 11 files changed, 52 insertions(+), 25 deletions(-) diff --git a/cmd/bootstrap/main.go b/cmd/bootstrap/main.go index 907a7fe38..45b371a1c 100644 --- a/cmd/bootstrap/main.go +++ b/cmd/bootstrap/main.go @@ -108,7 +108,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { "kademlia.bootstrap-addr": "localhost" + defaultServerAddr, } - return process.SaveConfig(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides) + return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides) } func main() { diff --git a/cmd/captplanet/setup.go b/cmd/captplanet/setup.go index b431e4c14..f33c5ba6f 100644 --- a/cmd/captplanet/setup.go +++ b/cmd/captplanet/setup.go @@ -154,7 +154,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { overrides[storagenode+"kademlia.alpha"] = 3 } - return process.SaveConfig(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides) + return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), overrides) } func joinHostPort(host string, port int) string { diff --git a/cmd/certificates/setup.go b/cmd/certificates/setup.go index 5cf4676d1..45e995a06 100644 --- a/cmd/certificates/setup.go +++ b/cmd/certificates/setup.go @@ -86,5 +86,5 @@ func cmdSetup(cmd *cobra.Command, args []string) error { "identity.key-path": setupCfg.Identity.KeyPath, "log.level": "info", } - return process.SaveConfig(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), o) + return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), o) } diff --git a/cmd/satellite/main.go b/cmd/satellite/main.go index 1db88c4e3..54db7b04e 100644 --- a/cmd/satellite/main.go +++ b/cmd/satellite/main.go @@ -180,7 +180,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { "identity.key-path": setupCfg.Identity.KeyPath, } - return process.SaveConfig(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), o) + return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), o) } func cmdDiag(cmd *cobra.Command, args []string) (err error) { diff --git a/cmd/storagenode/main.go b/cmd/storagenode/main.go index 16bf42696..6001d52c1 100644 --- a/cmd/storagenode/main.go +++ b/cmd/storagenode/main.go @@ -31,9 +31,10 @@ import ( // StorageNode defines storage node configuration type StorageNode struct { - CA identity.CASetupConfig `setup:"true"` - Identity identity.SetupConfig `setup:"true"` - EditConf bool `default:"false" help:"open config in default editor"` + CA identity.CASetupConfig `setup:"true"` + Identity identity.SetupConfig `setup:"true"` + EditConf bool `default:"false" help:"open config in default editor"` + SaveAllDefaults bool `default:"false" help:"save all default values to config.yaml file" setup:"true"` Server server.Config Kademlia kademlia.StorageNodeConfig @@ -164,7 +165,11 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { configFile := filepath.Join(setupDir, "config.yaml") - err = process.SaveConfig(cmd.Flags(), configFile, overrides) + if setupCfg.SaveAllDefaults { + err = process.SaveConfigWithAllDefaults(cmd.Flags(), configFile, overrides) + } else { + err = process.SaveConfig(cmd.Flags(), configFile, overrides) + } if err != nil { return err } diff --git a/cmd/uplink/cmd/setup.go b/cmd/uplink/cmd/setup.go index 5f3517f2b..955362894 100644 --- a/cmd/uplink/cmd/setup.go +++ b/cmd/uplink/cmd/setup.go @@ -129,7 +129,7 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) { "enc.key": setupCfg.EncKey, } - return process.SaveConfig(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), o) + return process.SaveConfigWithAllDefaults(cmd.Flags(), filepath.Join(setupDir, "config.yaml"), o) } func generateAWSKey() (key string, err error) { diff --git a/pkg/cfgstruct/bind.go b/pkg/cfgstruct/bind.go index 365d35cbc..7c31ae9dc 100644 --- a/pkg/cfgstruct/bind.go +++ b/pkg/cfgstruct/bind.go @@ -160,21 +160,24 @@ func bindConfig(flags FlagSet, prefix string, val reflect.Value, vars map[string panic(fmt.Sprintf("invalid field type: %s", field.Type.String())) } if onlyForSetup { - setSetupAnnotation(flags, flagname) + setBoolAnnotation(flags, flagname, "setup") + } + if field.Tag.Get("user") == "true" { + setBoolAnnotation(flags, flagname, "user") } } } } -func setSetupAnnotation(flagset interface{}, name string) { +func setBoolAnnotation(flagset interface{}, name, key string) { flags, ok := flagset.(*pflag.FlagSet) if !ok { return } - err := flags.SetAnnotation(name, "setup", []string{"true"}) + err := flags.SetAnnotation(name, key, []string{"true"}) if err != nil { - panic(fmt.Sprintf("unable to set annotation: %v", err)) + panic(fmt.Sprintf("unable to set %s annotation for %s: %v", key, name, err)) } } diff --git a/pkg/kademlia/config.go b/pkg/kademlia/config.go index cd2ec7c9c..67ec0472a 100644 --- a/pkg/kademlia/config.go +++ b/pkg/kademlia/config.go @@ -37,8 +37,8 @@ const ( // OperatorConfig defines properties related to storage node operator metadata type OperatorConfig struct { - Email string `help:"operator email address" default:""` - Wallet string `help:"operator wallet adress" default:""` + Email string `user:"true" help:"operator email address" default:""` + Wallet string `user:"true" help:"operator wallet adress" default:""` } // Config defines all of the things that are needed to start up Kademlia @@ -47,7 +47,7 @@ type Config struct { BootstrapAddr string `help:"the Kademlia node to bootstrap against" default:"127.0.0.1:7778"` DBPath string `help:"the path for storage node db services to be created on" default:"$CONFDIR/kademlia"` Alpha int `help:"alpha is a system wide concurrency parameter" default:"5"` - ExternalAddress string `help:"the public address of the Kademlia node, useful for nodes behind NAT" default:""` + ExternalAddress string `user:"true" help:"the public address of the Kademlia node, useful for nodes behind NAT" default:""` Operator OperatorConfig } diff --git a/pkg/piecestore/psserver/config.go b/pkg/piecestore/psserver/config.go index ff5434242..861d83b67 100644 --- a/pkg/piecestore/psserver/config.go +++ b/pkg/piecestore/psserver/config.go @@ -28,9 +28,9 @@ var ( // Config contains everything necessary for a server type Config struct { - Path string `help:"path to store data in" default:"$CONFDIR"` - AllocatedDiskSpace memory.Size `help:"total allocated disk space in bytes" default:"1TiB"` - AllocatedBandwidth memory.Size `help:"total allocated bandwidth in bytes" default:"500GiB"` + Path string `user:"true" help:"path to store data in" default:"$CONFDIR"` + AllocatedDiskSpace memory.Size `user:"true" help:"total allocated disk space in bytes" default:"1TiB"` + AllocatedBandwidth memory.Size `user:"true" help:"total allocated bandwidth in bytes" default:"500GiB"` KBucketRefreshInterval time.Duration `help:"how frequently Kademlia bucket should be refreshed with node stats" default:"1h0m0s"` AgreementSenderCheckInterval time.Duration `help:"duration between agreement checks" default:"1h0m0s"` } diff --git a/pkg/process/exec_conf.go b/pkg/process/exec_conf.go index d22160449..d87618e2b 100644 --- a/pkg/process/exec_conf.go +++ b/pkg/process/exec_conf.go @@ -52,9 +52,19 @@ var ( contexts = map[*cobra.Command]context.Context{} ) -// SaveConfig will save all flags with default values to outfilewith specific -// values specified in 'overrides' overridden. +// SaveConfig will save only the user-specific flags with default values to +// outfile with specific values specified in 'overrides' overridden. func SaveConfig(flagset *pflag.FlagSet, outfile string, overrides map[string]interface{}) error { + return saveConfig(flagset, outfile, overrides, false) +} + +// SaveConfigWithAllDefaults will save all flags with default values to outfile +// with specific values specified in 'overrides' overridden. +func SaveConfigWithAllDefaults(flagset *pflag.FlagSet, outfile string, overrides map[string]interface{}) error { + return saveConfig(flagset, outfile, overrides, true) +} + +func saveConfig(flagset *pflag.FlagSet, outfile string, overrides map[string]interface{}, saveAllDefaults bool) error { // we previously used Viper here, but switched to a custom serializer to allow comments //todo: switch back to Viper once go-yaml v3 is released and its supports writing comments? flagset.AddFlagSet(pflag.CommandLine) @@ -67,8 +77,10 @@ func SaveConfig(flagset *pflag.FlagSet, outfile string, overrides map[string]int w := &sb for _, k := range keys { f := flagset.Lookup(k) - setup := f.Annotations["setup"] - if len(setup) > 0 && setup[0] == "true" { + if readBoolAnnotation(f, "setup") { + continue + } + if !saveAllDefaults && !readBoolAnnotation(f, "user") && !f.Changed { continue } @@ -92,6 +104,11 @@ func SaveConfig(flagset *pflag.FlagSet, outfile string, overrides map[string]int return ioutil.WriteFile(outfile, []byte(sb.String()), os.FileMode(0644)) } +func readBoolAnnotation(flag *pflag.Flag, key string) bool { + annotation := flag.Annotations[key] + return len(annotation) > 0 && annotation[0] == "true" +} + // Ctx returns the appropriate context.Context for ExecuteWithConfig commands func Ctx(cmd *cobra.Command) context.Context { contextMtx.Lock() @@ -145,7 +162,6 @@ func cleanup(cmd *cobra.Command) { if err != nil { return err } - } } @@ -157,11 +173,14 @@ func cleanup(cmd *cobra.Command) { // flag couldn't be found brokenKeys = append(brokenKeys, key) } else { + oldChanged := cmd.Flag(key).Changed err := cmd.Flags().Set(key, vip.GetString(key)) if err != nil { // flag couldn't be set brokenVals = append(brokenVals, key) } + // revert Changed value + cmd.Flag(key).Changed = oldChanged } } diff --git a/pkg/server/config.go b/pkg/server/config.go index cd03a4fe1..4c92148b1 100644 --- a/pkg/server/config.go +++ b/pkg/server/config.go @@ -19,7 +19,7 @@ import ( type Config struct { RevocationDBURL string `help:"url for revocation database (e.g. bolt://some.db OR redis://127.0.0.1:6378?db=2&password=abc123)" default:"bolt://$CONFDIR/revocations.db"` PeerCAWhitelistPath string `help:"path to the CA cert whitelist (peer identities must be signed by one these to be verified)"` - Address string `help:"address to listen on" default:":7777"` + Address string `user:"true" help:"address to listen on" default:":7777"` Extensions peertls.TLSExtConfig Identity identity.Config