Save only user-specific flags to storage node's config.yaml (#1051)

This commit is contained in:
Kaloyan Raev 2019-01-15 15:55:33 +02:00 committed by GitHub
parent e38cf8f50d
commit a63abf8fab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 52 additions and 25 deletions

View File

@ -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() {

View File

@ -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 {

View File

@ -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)
}

View File

@ -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) {

View File

@ -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
}

View File

@ -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) {

View File

@ -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))
}
}

View File

@ -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
}

View File

@ -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"`
}

View File

@ -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
}
}

View File

@ -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