| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package config | 5 package config |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | |
| 9 "net/url" | 8 "net/url" |
| 10 | 9 |
| 11 "github.com/golang/protobuf/proto" | |
| 12 "github.com/luci/gae/service/info" | |
| 13 "github.com/luci/luci-go/common/config" | |
| 14 "github.com/luci/luci-go/common/errors" | 10 "github.com/luci/luci-go/common/errors" |
| 15 log "github.com/luci/luci-go/common/logging" | 11 log "github.com/luci/luci-go/common/logging" |
| 16 "github.com/luci/luci-go/logdog/api/config/svcconfig" | 12 "github.com/luci/luci-go/logdog/api/config/svcconfig" |
| 13 "github.com/luci/luci-go/server/config" |
| 14 "github.com/luci/luci-go/server/config/textproto" |
| 15 |
| 17 "golang.org/x/net/context" | 16 "golang.org/x/net/context" |
| 18 ) | 17 ) |
| 19 | 18 |
| 20 var ( | 19 var ( |
| 21 // ErrInvalidConfig is returned when the configuration exists, but is in
valid. | 20 // ErrInvalidConfig is returned when the configuration exists, but is in
valid. |
| 22 ErrInvalidConfig = errors.New("invalid configuration") | 21 ErrInvalidConfig = errors.New("invalid configuration") |
| 23 ) | 22 ) |
| 24 | 23 |
| 25 // Config is the LogDog Coordinator service configuration. | 24 // Config is the LogDog Coordinator service configuration. |
| 26 type Config struct { | 25 type Config struct { |
| 27 svcconfig.Config | 26 svcconfig.Config |
| 28 // Settings are per-instance settings. | 27 // Settings are per-instance settings. |
| 29 Settings Settings | 28 Settings Settings |
| 30 | 29 |
| 31 // ConfigServiceURL is the config service's URL. | 30 // ConfigServiceURL is the config service's URL. |
| 32 ConfigServiceURL url.URL `json:"-"` | 31 ConfigServiceURL url.URL `json:"-"` |
| 33 // ConfigSet is the name of the service config set that is being used. | 32 // ConfigSet is the name of the service config set that is being used. |
| 34 ConfigSet string `json:"-"` | 33 ConfigSet string `json:"-"` |
| 35 // ServiceConfigPath is the path within ConfigSet of the service | 34 // ServiceConfigPath is the path within ConfigSet of the service |
| 36 // configuration. | 35 // configuration. |
| 37 ServiceConfigPath string `json:"-"` | 36 ServiceConfigPath string `json:"-"` |
| 38 } | 37 } |
| 39 | 38 |
| 40 // ServiceConfigPath returns the config set and path for this application's | 39 // ServiceConfigPath returns the config set and path for this application's |
| 41 // service configuration. | 40 // service configuration. |
| 42 func ServiceConfigPath(c context.Context) (string, string) { | 41 func ServiceConfigPath(c context.Context) (string, string) { |
| 43 » appID := info.AppID(c) | 42 » return config.CurrentServiceConfigSet(c), svcconfig.ServiceConfigFilenam
e |
| 44 » return fmt.Sprintf("services/%s", appID), svcconfig.ServiceConfigFilenam
e | |
| 45 } | 43 } |
| 46 | 44 |
| 47 // Load loads the service configuration. This includes: | 45 // Load loads the service configuration. This includes: |
| 48 // - The config service settings. | 46 // - The config service settings. |
| 49 // - The service configuration, loaded from the config service. | 47 // - The service configuration, loaded from the config service. |
| 50 // - Additional Settings data from datastore via settings. | 48 // - Additional Settings data from datastore via settings. |
| 51 // | 49 // |
| 52 // The service config is minimally validated prior to being returned. | 50 // The service config is minimally validated prior to being returned. |
| 53 func Load(c context.Context) (*Config, error) { | 51 func Load(c context.Context) (*Config, error) { |
| 54 » configSet, configPath := ServiceConfigPath(c) | 52 » // Unmarshal the config into service configuration. |
| 55 » serviceCfg, err := config.GetConfig(c, configSet, configPath, false) | 53 » cfg := Config{ |
| 56 » if err != nil { | 54 » » ConfigServiceURL: config.ServiceURL(c), |
| 55 » } |
| 56 » cfg.ConfigSet, cfg.ServiceConfigPath = ServiceConfigPath(c) |
| 57 |
| 58 » // Load our service-level config. |
| 59 » if err := config.Get(c, config.AsService, cfg.ConfigSet, cfg.ServiceConf
igPath, |
| 60 » » textproto.Message(&cfg.Config), nil); err != nil { |
| 61 |
| 57 log.Fields{ | 62 log.Fields{ |
| 58 log.ErrorKey: err, | 63 log.ErrorKey: err, |
| 59 » » » "configSet": configSet, | 64 » » » "configSet": cfg.ConfigSet, |
| 60 » » » "configPath": configPath, | 65 » » » "configPath": cfg.ServiceConfigPath, |
| 61 }.Errorf(c, "Failed to load configuration from config service.") | 66 }.Errorf(c, "Failed to load configuration from config service.") |
| 62 return nil, err | 67 return nil, err |
| 63 } | 68 } |
| 64 | 69 |
| 65 // Unmarshal the config into service configuration. | |
| 66 cfg := Config{ | |
| 67 ConfigServiceURL: config.ServiceURL(c), | |
| 68 ConfigSet: serviceCfg.ConfigSet, | |
| 69 ServiceConfigPath: serviceCfg.Path, | |
| 70 } | |
| 71 | |
| 72 if err := proto.UnmarshalText(serviceCfg.Content, &cfg.Config); err != n
il { | |
| 73 log.Fields{ | |
| 74 log.ErrorKey: err, | |
| 75 "size": len(serviceCfg.Content), | |
| 76 "contentHash": serviceCfg.ContentHash, | |
| 77 "configSet": serviceCfg.ConfigSet, | |
| 78 "revision": serviceCfg.Revision, | |
| 79 }.Errorf(c, "Failed to unmarshal configuration protobuf.") | |
| 80 return nil, ErrInvalidConfig | |
| 81 } | |
| 82 | |
| 83 // Validate the configuration. | 70 // Validate the configuration. |
| 84 if err := validateServiceConfig(&cfg.Config); err != nil { | 71 if err := validateServiceConfig(&cfg.Config); err != nil { |
| 85 log.WithError(err).Errorf(c, "Invalid Coordinator configuration.
") | 72 log.WithError(err).Errorf(c, "Invalid Coordinator configuration.
") |
| 86 return nil, ErrInvalidConfig | 73 return nil, ErrInvalidConfig |
| 87 } | 74 } |
| 88 | 75 |
| 89 // Load our settings. | 76 // Load our settings. |
| 90 if err := cfg.Settings.Load(c); err != nil { | 77 if err := cfg.Settings.Load(c); err != nil { |
| 91 log.WithError(err).Errorf(c, "Failed to load settings.") | 78 log.WithError(err).Errorf(c, "Failed to load settings.") |
| 92 return nil, ErrInvalidConfig | 79 return nil, ErrInvalidConfig |
| 93 } | 80 } |
| 94 | 81 |
| 95 return &cfg, nil | 82 return &cfg, nil |
| 96 } | 83 } |
| 97 | 84 |
| 98 // validateServiceConfig checks the supplied service config object to ensure | 85 // validateServiceConfig checks the supplied service config object to ensure |
| 99 // that it meets a minimum configuration standard expected by our endpoitns and | 86 // that it meets a minimum configuration standard expected by our endpoitns and |
| 100 // handlers. | 87 // handlers. |
| 101 func validateServiceConfig(cc *svcconfig.Config) error { | 88 func validateServiceConfig(cc *svcconfig.Config) error { |
| 102 switch { | 89 switch { |
| 103 case cc == nil: | 90 case cc == nil: |
| 104 return errors.New("configuration is nil") | 91 return errors.New("configuration is nil") |
| 105 case cc.GetCoordinator() == nil: | 92 case cc.GetCoordinator() == nil: |
| 106 return errors.New("no Coordinator configuration") | 93 return errors.New("no Coordinator configuration") |
| 107 default: | 94 default: |
| 108 return nil | 95 return nil |
| 109 } | 96 } |
| 110 } | 97 } |
| OLD | NEW |