| 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" | 8 "fmt" |
| 9 "strings" | 9 "strings" |
| 10 | 10 |
| 11 "github.com/golang/protobuf/proto" | |
| 12 "github.com/luci/gae/service/info" | 11 "github.com/luci/gae/service/info" |
| 13 "github.com/luci/luci-go/common/config" | 12 "github.com/luci/luci-go/common/config" |
| 14 log "github.com/luci/luci-go/common/logging" | 13 log "github.com/luci/luci-go/common/logging" |
| 15 "github.com/luci/luci-go/logdog/api/config/svcconfig" | 14 "github.com/luci/luci-go/logdog/api/config/svcconfig" |
| 15 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 16 |
| 17 "github.com/golang/protobuf/proto" |
| 16 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
| 17 ) | 19 ) |
| 18 | 20 |
| 19 const maxProjectWorkers = 32 | 21 const maxProjectWorkers = 32 |
| 20 | 22 |
| 21 // ProjectConfigPath returns the path of the project-specific configuration. | 23 // ProjectConfigPath returns the path of the project-specific configuration. |
| 22 // This path should be used with a project config set. | 24 // This path should be used with a project config set. |
| 23 // | 25 // |
| 24 // A given project's configuration is named after the current App ID. | 26 // A given project's configuration is named after the current App ID. |
| 25 func ProjectConfigPath(c context.Context) string { | 27 func ProjectConfigPath(c context.Context) string { |
| 26 return fmt.Sprintf("%s.cfg", info.AppID(c)) | 28 return fmt.Sprintf("%s.cfg", info.AppID(c)) |
| 27 } | 29 } |
| 28 | 30 |
| 29 // ProjectConfig loads the project config protobuf from the config service. | 31 // ProjectConfig loads the project config protobuf from the config service. |
| 30 // | 32 // |
| 31 // This function will return: | 33 // This function will return: |
| 32 // - nil, if the project exists and the configuration successfully loaded | 34 // - nil, if the project exists and the configuration successfully loaded |
| 33 // - config.ErrNoConfig if the project configuration was not present. | 35 // - config.ErrNoConfig if the project configuration was not present. |
| 34 // - ErrInvalidConfig if the project configuration was present, but could n
ot | 36 // - ErrInvalidConfig if the project configuration was present, but could n
ot |
| 35 // be loaded. | 37 // be loaded. |
| 36 // - Some other error if an error occurred that does not fit one of the | 38 // - Some other error if an error occurred that does not fit one of the |
| 37 // previous categories. | 39 // previous categories. |
| 38 func ProjectConfig(c context.Context, project config.ProjectName) (*svcconfig.Pr
ojectConfig, error) { | 40 func ProjectConfig(c context.Context, project cfgtypes.ProjectName) (*svcconfig.
ProjectConfig, error) { |
| 39 if project == "" { | 41 if project == "" { |
| 40 return nil, config.ErrNoConfig | 42 return nil, config.ErrNoConfig |
| 41 } | 43 } |
| 42 | 44 |
| 43 // Get the config from the config service. If the configuration doesn't
exist, | 45 // Get the config from the config service. If the configuration doesn't
exist, |
| 44 // this will return config.ErrNoConfig. | 46 // this will return config.ErrNoConfig. |
| 45 » configSet, configPath := config.ProjectConfigSet(project), ProjectConfig
Path(c) | 47 » configSet, configPath := cfgtypes.ProjectConfigSet(project), ProjectConf
igPath(c) |
| 46 » cfg, err := config.GetConfig(c, configSet, configPath, false) | 48 » cfg, err := config.GetConfig(c, string(configSet), configPath, false) |
| 47 if err != nil { | 49 if err != nil { |
| 48 log.Fields{ | 50 log.Fields{ |
| 49 log.ErrorKey: err, | 51 log.ErrorKey: err, |
| 50 "project": project, | 52 "project": project, |
| 51 "configSet": configSet, | 53 "configSet": configSet, |
| 52 "configPath": configPath, | 54 "configPath": configPath, |
| 53 }.Errorf(c, "Failed to load project configuration content.") | 55 }.Errorf(c, "Failed to load project configuration content.") |
| 54 return nil, err | 56 return nil, err |
| 55 } | 57 } |
| 56 | 58 |
| 57 pcfg, err := unmarshalProjectConfig(cfg) | 59 pcfg, err := unmarshalProjectConfig(cfg) |
| 58 if err != nil { | 60 if err != nil { |
| 59 log.Fields{ | 61 log.Fields{ |
| 60 log.ErrorKey: err, | 62 log.ErrorKey: err, |
| 61 "project": project, | 63 "project": project, |
| 62 "configSet": cfg.ConfigSet, | 64 "configSet": cfg.ConfigSet, |
| 63 "path": cfg.Path, | 65 "path": cfg.Path, |
| 64 "contentHash": cfg.ContentHash, | 66 "contentHash": cfg.ContentHash, |
| 65 }.Errorf(c, "Failed to unmarshal project configuration.") | 67 }.Errorf(c, "Failed to unmarshal project configuration.") |
| 66 return nil, ErrInvalidConfig | 68 return nil, ErrInvalidConfig |
| 67 } | 69 } |
| 68 return pcfg, nil | 70 return pcfg, nil |
| 69 } | 71 } |
| 70 | 72 |
| 71 // AllProjectConfigs returns the project configurations for all projects that | 73 // AllProjectConfigs returns the project configurations for all projects that |
| 72 // have a configuration. | 74 // have a configuration. |
| 73 // | 75 // |
| 74 // If a project's configuration fails to load, an error will be logged and the | 76 // If a project's configuration fails to load, an error will be logged and the |
| 75 // project will be omitted from the output map. | 77 // project will be omitted from the output map. |
| 76 func AllProjectConfigs(c context.Context) (map[config.ProjectName]*svcconfig.Pro
jectConfig, error) { | 78 func AllProjectConfigs(c context.Context) (map[cfgtypes.ProjectName]*svcconfig.P
rojectConfig, error) { |
| 77 // TODO: This endpoint is generally slow. Even though there is memcache-
based | 79 // TODO: This endpoint is generally slow. Even though there is memcache-
based |
| 78 // config cache, this really should be loaded from a more failsafe cache
like | 80 // config cache, this really should be loaded from a more failsafe cache
like |
| 79 // datastore to protect against config service outages. | 81 // datastore to protect against config service outages. |
| 80 configs, err := config.GetProjectConfigs(c, ProjectConfigPath(c), false) | 82 configs, err := config.GetProjectConfigs(c, ProjectConfigPath(c), false) |
| 81 if err != nil { | 83 if err != nil { |
| 82 log.WithError(err).Errorf(c, "Failed to load project configs.") | 84 log.WithError(err).Errorf(c, "Failed to load project configs.") |
| 83 return nil, err | 85 return nil, err |
| 84 } | 86 } |
| 85 | 87 |
| 86 » result := make(map[config.ProjectName]*svcconfig.ProjectConfig, len(conf
igs)) | 88 » result := make(map[cfgtypes.ProjectName]*svcconfig.ProjectConfig, len(co
nfigs)) |
| 87 for _, cfg := range configs { | 89 for _, cfg := range configs { |
| 88 // Identify the project by removng the "projects/" prefix. | 90 // Identify the project by removng the "projects/" prefix. |
| 89 » » project := config.ProjectName(strings.TrimPrefix(cfg.ConfigSet,
"projects/")) | 91 » » project := cfgtypes.ProjectName(strings.TrimPrefix(cfg.ConfigSet
, "projects/")) |
| 90 if err := project.Validate(); err != nil { | 92 if err := project.Validate(); err != nil { |
| 91 log.Fields{ | 93 log.Fields{ |
| 92 log.ErrorKey: err, | 94 log.ErrorKey: err, |
| 93 "configSet": cfg.ConfigSet, | 95 "configSet": cfg.ConfigSet, |
| 94 }.Errorf(c, "Invalid project name returned.") | 96 }.Errorf(c, "Invalid project name returned.") |
| 95 continue | 97 continue |
| 96 } | 98 } |
| 97 | 99 |
| 98 // Unmarshal the project's configuration. | 100 // Unmarshal the project's configuration. |
| 99 pcfg, err := unmarshalProjectConfig(&cfg) | 101 pcfg, err := unmarshalProjectConfig(&cfg) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 112 return result, nil | 114 return result, nil |
| 113 } | 115 } |
| 114 | 116 |
| 115 func unmarshalProjectConfig(cfg *config.Config) (*svcconfig.ProjectConfig, error
) { | 117 func unmarshalProjectConfig(cfg *config.Config) (*svcconfig.ProjectConfig, error
) { |
| 116 var pcfg svcconfig.ProjectConfig | 118 var pcfg svcconfig.ProjectConfig |
| 117 if err := proto.UnmarshalText(cfg.Content, &pcfg); err != nil { | 119 if err := proto.UnmarshalText(cfg.Content, &pcfg); err != nil { |
| 118 return nil, err | 120 return nil, err |
| 119 } | 121 } |
| 120 return &pcfg, nil | 122 return &pcfg, nil |
| 121 } | 123 } |
| OLD | NEW |