| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package config | |
| 6 | |
| 7 import ( | |
| 8 "errors" | |
| 9 "flag" | |
| 10 "fmt" | |
| 11 "net/http" | |
| 12 "net/url" | |
| 13 "time" | |
| 14 | |
| 15 "github.com/luci/luci-go/common/clock/clockflag" | |
| 16 "github.com/luci/luci-go/common/config" | |
| 17 "github.com/luci/luci-go/common/config/impl/filesystem" | |
| 18 "github.com/luci/luci-go/common/config/impl/remote" | |
| 19 "github.com/luci/luci-go/common/proto/google" | |
| 20 "github.com/luci/luci-go/logdog/api/endpoints/coordinator/services/v1" | |
| 21 "golang.org/x/net/context" | |
| 22 ) | |
| 23 | |
| 24 // Flags is the set of command-line flags used to configure a service. | |
| 25 type Flags struct { | |
| 26 // ConfigFilePath is the path within the ConfigSet of the configuration. | |
| 27 // | |
| 28 // If ConfigURL is empty, this will be interpreted as a local filesystem
path | |
| 29 // from which the configuration should be loaded. | |
| 30 ConfigFilePath string | |
| 31 | |
| 32 // KillCheckInterval, if >0, starts a goroutine that polls every interva
l to | |
| 33 // see if the configuration has changed. | |
| 34 KillCheckInterval clockflag.Duration | |
| 35 | |
| 36 // RoundTripper, if not nil, is the http.RoundTripper that will be used
to | |
| 37 // fetch remote configurations. | |
| 38 RoundTripper http.RoundTripper | |
| 39 } | |
| 40 | |
| 41 // AddToFlagSet augments the supplied FlagSet with values for Flags. | |
| 42 func (f *Flags) AddToFlagSet(fs *flag.FlagSet) { | |
| 43 fs.StringVar(&f.ConfigFilePath, "config-file-path", "", | |
| 44 "If set, load configuration from a local filesystem rooted here.
") | |
| 45 fs.Var(&f.KillCheckInterval, "config-kill-interval", | |
| 46 "If non-zero, poll for configuration changes and kill the applic
ation if one is detected.") | |
| 47 } | |
| 48 | |
| 49 // CoordinatorOptions returns an Options instance loaded from the supplied flags | |
| 50 // and Coordinator instance. | |
| 51 func (f *Flags) CoordinatorOptions(c context.Context, client logdog.ServicesClie
nt) (*Options, error) { | |
| 52 ccfg, err := client.GetConfig(c, &google.Empty{}) | |
| 53 if err != nil { | |
| 54 return nil, err | |
| 55 } | |
| 56 | |
| 57 // If a ConfigFilePath was specified, use a mock configuration service t
hat | |
| 58 // loads from a local file. | |
| 59 var ci config.Interface | |
| 60 if f.ConfigFilePath != "" { | |
| 61 var err error | |
| 62 ci, err = filesystem.New(f.ConfigFilePath) | |
| 63 if err != nil { | |
| 64 return nil, err | |
| 65 } | |
| 66 } else { | |
| 67 host := ccfg.ConfigServiceHost | |
| 68 if host == "" { | |
| 69 if ccfg.ConfigServiceUrl == "" { | |
| 70 return nil, errors.New("coordinator does not spe
cify a config service") | |
| 71 } | |
| 72 u, err := url.Parse(ccfg.ConfigServiceUrl) | |
| 73 if err != nil { | |
| 74 return nil, fmt.Errorf("failed to parse config s
ervice URL: %v", err) | |
| 75 } | |
| 76 host = u.Host | |
| 77 } | |
| 78 if ccfg.ConfigSet == "" { | |
| 79 return nil, errors.New("coordinator does not specify a c
onfig set") | |
| 80 } | |
| 81 if ccfg.ServiceConfigPath == "" { | |
| 82 return nil, errors.New("coordinator does not specify a c
onfig path") | |
| 83 } | |
| 84 | |
| 85 var clientFactory remote.ClientFactory | |
| 86 if f.RoundTripper != nil { | |
| 87 rr := f.RoundTripper | |
| 88 clientFactory = func(context.Context) (*http.Client, err
or) { | |
| 89 return &http.Client{Transport: rr}, nil | |
| 90 } | |
| 91 } | |
| 92 ci = remote.New(host, false, clientFactory) | |
| 93 } | |
| 94 | |
| 95 return &Options{ | |
| 96 Config: ci, | |
| 97 ConfigSet: ccfg.ConfigSet, | |
| 98 ServiceConfigPath: ccfg.ServiceConfigPath, | |
| 99 KillCheckInterval: time.Duration(f.KillCheckInterval), | |
| 100 }, nil | |
| 101 } | |
| OLD | NEW |