| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package config | 5 package config |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 | 9 |
| 10 "github.com/golang/protobuf/proto" | 10 "github.com/golang/protobuf/proto" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 // Config, is set, is the application's text-format configuration protob
uf. | 52 // Config, is set, is the application's text-format configuration protob
uf. |
| 53 // This is only used when running under dev-appserver. | 53 // This is only used when running under dev-appserver. |
| 54 Config string `gae:",noindex"` | 54 Config string `gae:",noindex"` |
| 55 } | 55 } |
| 56 | 56 |
| 57 // LoadGlobalConfig loads the global configuration from the AppEngine settings. | 57 // LoadGlobalConfig loads the global configuration from the AppEngine settings. |
| 58 // | 58 // |
| 59 // If no global configuration is present, settings.ErrNoSettings will be | 59 // If no global configuration is present, settings.ErrNoSettings will be |
| 60 // returned. | 60 // returned. |
| 61 func LoadGlobalConfig(c context.Context) (*GlobalConfig, error) { | 61 func LoadGlobalConfig(c context.Context) (*GlobalConfig, error) { |
| 62 » gc := GlobalConfig{} | 62 » gcfg := GlobalConfig{} |
| 63 » if err := settings.Get(c, globalConfigSettingsKey, &gc); err != nil { | 63 » if err := settings.Get(c, globalConfigSettingsKey, &gcfg); err != nil { |
| 64 » » // If we're running the dev appserver, install some configuratio
n stubs so |
| 65 » » // they can be tweaked locally. |
| 66 » » if err == settings.ErrNoSettings && info.Get(c).IsDevAppServer()
{ |
| 67 » » » log.Infof(c, "Setting up development configuration...") |
| 68 |
| 69 » » » if err := gcfg.storeNoValidate(c, "development setup");
err != nil { |
| 70 » » » » log.WithError(err).Warningf(c, "Failed to instal
l development global config stub.") |
| 71 » » » } |
| 72 » » » if _, err := getSetupDevConfig(c); err != nil { |
| 73 » » » » log.WithError(err).Warningf(c, "Failed to instal
l development configuration entry stub.") |
| 74 » » » } |
| 75 » » } |
| 76 |
| 64 return nil, err | 77 return nil, err |
| 65 } | 78 } |
| 66 » return &gc, nil | 79 » return &gcfg, nil |
| 67 } | 80 } |
| 68 | 81 |
| 69 // Store stores the new global configuration. | 82 // Store stores the new global configuration. |
| 70 func (gcfg *GlobalConfig) Store(c context.Context, why string) error { | 83 func (gcfg *GlobalConfig) Store(c context.Context, why string) error { |
| 71 if err := gcfg.Validate(); err != nil { | 84 if err := gcfg.Validate(); err != nil { |
| 72 return err | 85 return err |
| 73 } | 86 } |
| 74 return gcfg.storeNoValidate(c, why) | 87 return gcfg.storeNoValidate(c, why) |
| 75 } | 88 } |
| 76 | 89 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 | 162 |
| 150 // Validate the configuration. | 163 // Validate the configuration. |
| 151 if err := validateConfig(&cc); err != nil { | 164 if err := validateConfig(&cc); err != nil { |
| 152 log.WithError(err).Errorf(c, "Invalid Coordinator configuration.
") | 165 log.WithError(err).Errorf(c, "Invalid Coordinator configuration.
") |
| 153 return nil, settings.ErrNoSettings | 166 return nil, settings.ErrNoSettings |
| 154 } | 167 } |
| 155 | 168 |
| 156 return &cc, nil | 169 return &cc, nil |
| 157 } | 170 } |
| 158 | 171 |
| 159 // Load loads the current configuration from "luci-config". | |
| 160 func Load(c context.Context) (*svcconfig.Config, error) { | |
| 161 gcfg, err := LoadGlobalConfig(c) | |
| 162 if err != nil { | |
| 163 // If we're running the dev appserver, install some configuratio
n stubs so | |
| 164 // they can be tweaked locally. | |
| 165 if err == settings.ErrNoSettings { | |
| 166 if info.Get(c).IsDevAppServer() { | |
| 167 log.Infof(c, "Setting up development configurati
on...") | |
| 168 gcfg = &GlobalConfig{} | |
| 169 if err := gcfg.storeNoValidate(c, "development s
etup"); err != nil { | |
| 170 log.WithError(err).Warningf(c, "Failed t
o install development global config stub.") | |
| 171 } | |
| 172 if _, err := getSetupDevConfig(c); err != nil { | |
| 173 log.WithError(err).Warningf(c, "Failed t
o install development configuration entry stub.") | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 return nil, err | |
| 179 } | |
| 180 return gcfg.LoadConfig(c) | |
| 181 } | |
| 182 | |
| 183 // validateConfig checks the supplied Coordinator config object to ensure that | 172 // validateConfig checks the supplied Coordinator config object to ensure that |
| 184 // it meets a minimum configuration standard expected by our endpoitns and | 173 // it meets a minimum configuration standard expected by our endpoitns and |
| 185 // handlers. | 174 // handlers. |
| 186 func validateConfig(cc *svcconfig.Config) error { | 175 func validateConfig(cc *svcconfig.Config) error { |
| 187 switch { | 176 switch { |
| 188 case cc == nil: | 177 case cc == nil: |
| 189 return errors.New("configuration is nil") | 178 return errors.New("configuration is nil") |
| 190 case cc.GetCoordinator() == nil: | 179 case cc.GetCoordinator() == nil: |
| 191 return errors.New("no Coordinator configuration") | 180 return errors.New("no Coordinator configuration") |
| 192 default: | 181 default: |
| (...skipping 15 matching lines...) Expand all Loading... |
| 208 return nil, errors.New("no development configuration") | 197 return nil, errors.New("no development configuration") |
| 209 | 198 |
| 210 default: | 199 default: |
| 211 return nil, err | 200 return nil, err |
| 212 } | 201 } |
| 213 | 202 |
| 214 return &config.Config{ | 203 return &config.Config{ |
| 215 Content: dcfg.Config, | 204 Content: dcfg.Config, |
| 216 }, nil | 205 }, nil |
| 217 } | 206 } |
| OLD | NEW |