@@ -17,13 +17,10 @@ package main
17
17
import (
18
18
"flag"
19
19
"fmt"
20
- "io/ioutil"
21
20
"net"
22
21
"net/http"
23
22
"os"
24
- "os/signal"
25
23
"strings"
26
- "syscall"
27
24
"time"
28
25
29
26
"golang.org/x/net/context"
@@ -34,6 +31,7 @@ import (
34
31
rpc "google.golang.org/grpc"
35
32
"google.golang.org/grpc/credentials"
36
33
34
+ "github.com/youtube/doorman/go/configuration"
37
35
"github.com/youtube/doorman/go/connection"
38
36
"github.com/youtube/doorman/go/server/doorman"
39
37
"github.com/youtube/doorman/go/server/election"
55
53
serverRole = flag .String ("server_role" , "root" , "Role of this server in the server tree" )
56
54
parent = flag .String ("parent" , "" , "Address of the parent server which this server connects to" )
57
55
hostname = flag .String ("hostname" , "" , "Use this as the hostname (if empty, use whatever the kernel reports" )
58
- config = flag .String ("config" , "" , "file to load the config from (text protobufs)" )
56
+ config = flag .String ("config" , "" , "source to load the config from (text protobufs)" )
59
57
60
58
rpcDialTimeout = flag .Duration ("doorman_rpc_dial_timeout" , 5 * time .Second , "timeout to use for connecting to the doorman server" )
61
59
@@ -142,10 +140,12 @@ func main() {
142
140
if * config == "" {
143
141
log .Exit ("--config cannot be empty" )
144
142
}
145
-
146
- var masterElection election.Election
143
+ var (
144
+ etcdEndpointsSlice = strings .Split (* etcdEndpoints , "," )
145
+ masterElection election.Election
146
+ )
147
147
if * masterElectionLock != "" {
148
- etcdEndpointsSlice := strings . Split ( * etcdEndpoints , "," )
148
+
149
149
if len (etcdEndpointsSlice ) == 1 && etcdEndpointsSlice [0 ] == "" {
150
150
log .Exit ("-etcd_endpoints cannot be empty if -master_election_lock is provided" )
151
151
}
@@ -180,42 +180,39 @@ func main() {
180
180
log .Exit ("-config cannot be empty" )
181
181
}
182
182
183
- data , err := ioutil . ReadFile ( * config )
184
- if err != nil {
185
- log . Exitf ( "cannot read config file: %v" , err )
186
- }
187
-
188
- cfg := new (pb. ResourceRepository )
189
- if err := proto . UnmarshalText ( string ( data ), cfg ); err != nil {
190
- log .Exitf ( " cannot load config: %v" , err )
191
- }
192
-
193
- if err := dm . LoadConfig ( context . Background (), cfg , map [ string ] * time. Time {}); err != nil {
194
- log . Exitf ( "dm.LoadConfig: %v" , err )
183
+ var cfg configuration. Source
184
+ kind , path := configuration . ParseSource ( * config )
185
+ switch {
186
+ case kind == "file" :
187
+ cfg = configuration . LocalFile ( path )
188
+ case kind == "etcd" :
189
+ if len ( etcdEndpointsSlice ) == 1 && etcdEndpointsSlice [ 0 ] == "" {
190
+ log .Exit ( "-etcd_endpoints cannot be empty if a config source etcd is provided" )
191
+ }
192
+ cfg = configuration . Etcd ( path , etcdEndpointsSlice )
193
+ default :
194
+ panic ( "unreachable" )
195
195
}
196
196
197
- c := make ( chan os. Signal , 1 )
198
- signal . Notify ( c , syscall . SIGHUP )
199
-
197
+ // Try to load the background. If there's a problem with loading
198
+ // the server for the first time, the server will keep running,
199
+ // but will not serve traffic.
200
200
go func () {
201
- for range c {
202
- log .Infof ("Received SIGHUP, attempting to reload configuration from %v." , * config )
203
- data , err := ioutil .ReadFile (* config )
201
+ for {
202
+ data , err := cfg (context .Background ())
204
203
if err != nil {
205
- log .Errorf ("cannot read config file : %v" , err )
204
+ log .Errorf ("cannot load config data : %v" , err )
206
205
continue
207
206
}
208
-
209
207
cfg := new (pb.ResourceRepository )
210
208
if err := proto .UnmarshalText (string (data ), cfg ); err != nil {
211
- log .Errorf ("cannot load config: %v " , err )
209
+ log .Errorf ("cannot unmarshal config data : %q " , data )
212
210
continue
213
211
}
214
212
215
213
if err := dm .LoadConfig (context .Background (), cfg , map [string ]* time.Time {}); err != nil {
216
- log .Errorf ("dm.LoadConfig : %v" , err )
214
+ log .Errorf ("cannot load config : %v" , err )
217
215
}
218
- log .Infof ("Reloaded config from %v" , * config )
219
216
}
220
217
}()
221
218
0 commit comments