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