Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package config | |
| 6 | |
| 7 import ( | |
| 8 "errors" | |
| 9 "flag" | |
| 10 "net/http" | |
| 11 "time" | |
| 12 | |
| 13 "github.com/luci/luci-go/common/clock/clockflag" | |
| 14 "github.com/luci/luci-go/common/config" | |
| 15 "github.com/luci/luci-go/common/config/impl/remote" | |
| 16 "github.com/luci/luci-go/common/transport" | |
| 17 cc "github.com/luci/luci-go/server/internal/logdog/coordinatorClient" | |
| 18 "golang.org/x/net/context" | |
| 19 ) | |
| 20 | |
| 21 // CoordinatorConfigGetter is an interface to fetch the Coordinator's service | |
| 22 // configuration. | |
| 23 // | |
| 24 // coordinatorClient.Client is an implementation of this functionality. | |
| 25 type CoordinatorConfigGetter interface { | |
| 26 // GetConfig returns the Coordinator's service configuration. | |
| 27 GetConfig(c context.Context) (*cc.ServiceConfig, error) | |
| 28 } | |
| 29 | |
| 30 // Flags is a set of command-line flags used to set up the process' | |
| 31 // configuration. | |
| 32 type Flags struct { | |
| 33 // ConfigPath is the path within the ConfigSet of the configuration. | |
| 34 // | |
| 35 // If ConfigURL is empty, this will be interpreted as a local filesystem path | |
| 36 // from which the configuration should be loaded. | |
| 37 ConfigFilePath string | |
| 38 | |
| 39 // KillCheckInterval, if >0, starts a goroutine that polls every interva l to | |
| 40 // see if the configuration has changed. | |
| 41 KillCheckInterval clockflag.Duration | |
| 42 | |
| 43 // RoundTripper, if not nil, is the http.RoundTripper that will be used to | |
| 44 // fetch remote configurations. | |
| 45 RoundTripper http.RoundTripper | |
| 46 } | |
| 47 | |
| 48 // AddToFlagSet augments the supplied FlagSet with values for Flags. | |
| 49 func (f *Flags) AddToFlagSet(fs *flag.FlagSet) { | |
| 50 fs.StringVar(&f.ConfigFilePath, "config-file-path", "", | |
| 51 "If set, load the configuration protobuf from this path instead of the configuration service.") | |
| 52 fs.Var(&f.KillCheckInterval, "config-kill-interval", | |
| 53 "If non-zero, poll for configuration changes and kill the applic ation if one is detected.") | |
| 54 } | |
| 55 | |
| 56 // CoordinatorOptions returns an Options instance loaded from the supplied flags | |
| 57 // and Coordinator instance. | |
| 58 func (f *Flags) CoordinatorOptions(c context.Context, cc CoordinatorConfigGetter ) (*Options, error) { | |
| 59 ccfg, err := cc.GetConfig(c) | |
| 60 if err != nil { | |
| 61 return nil, err | |
| 62 } | |
| 63 | |
| 64 // If a ConfigFilePath was specified, use a mock configuration service t hat | |
| 65 // loads from a local file. | |
| 66 var ci config.Interface | |
| 67 if f.ConfigFilePath != "" { | |
| 68 ci = &fileConfig{ | |
| 69 path: f.ConfigFilePath, | |
| 70 configSet: ccfg.ConfigSet, | |
| 71 configPath: ccfg.ConfigPath, | |
| 72 } | |
| 73 } else { | |
| 74 if ccfg.ConfigService == "" { | |
|
martiniss
2016/01/26 02:24:18
Why do we check these here? Why not just pass it t
dnj (Google)
2016/01/26 05:06:05
I don't really see value to letting the remote lib
| |
| 75 return nil, errors.New("coordinator does not specify a c onfig service") | |
| 76 } | |
| 77 if ccfg.ConfigSet == "" { | |
| 78 return nil, errors.New("coordinator does not specify a c onfig set") | |
| 79 } | |
| 80 if ccfg.ConfigPath == "" { | |
| 81 return nil, errors.New("coordinator does not specify a c onfig path") | |
| 82 } | |
| 83 | |
| 84 if f.RoundTripper != nil { | |
| 85 c = transport.Set(c, f.RoundTripper) | |
| 86 } | |
| 87 ci = remote.New(c, ccfg.ConfigService) | |
| 88 } | |
| 89 | |
| 90 return &Options{ | |
| 91 Config: ci, | |
| 92 ConfigSet: ccfg.ConfigSet, | |
| 93 ConfigPath: ccfg.ConfigPath, | |
| 94 KillCheckInterval: time.Duration(f.KillCheckInterval), | |
| 95 }, nil | |
| 96 } | |
| OLD | NEW |