@@ -4,8 +4,10 @@ import (
44 "flag"
55 "fmt"
66 "os"
7+ "os/signal"
78 "strconv"
89 "sync"
10+ "syscall"
911 "time"
1012
1113 "github.com/BurntSushi/toml"
@@ -36,8 +38,9 @@ type config struct {
3638 ZMQPrivateKeyPath string `toml:"zmq_privkey_path"`
3739 StationPublicKeys []string `toml:"station_pubkeys"`
3840 ClientConfPath string `toml:"clientconf_path"`
39- LogLevel string `toml:"log_level"`
40- LogMetricsInterval uint16 `toml:"log_metrics_interval"`
41+ latestClientConf * pb.ClientConf
42+ LogLevel string `toml:"log_level"`
43+ LogMetricsInterval uint16 `toml:"log_metrics_interval"`
4144}
4245
4346// parseClientConf parse the latest ClientConf based on path file
@@ -108,6 +111,27 @@ func readKeyAndEncode(path string) (string, error) {
108111 return privkey , nil
109112}
110113
114+ // loadConfig is intended to re-parse portions of the config in conjunction with
115+ // setupReloadHandler. This is specifically for settings where we do not want to
116+ // restart the station. This is not intended to be a full re-build of the
117+ // station (i.e. auth, workers, and loglevel are not changed), Mostly this
118+ // should allow us to dynamically reload when there is an update to the latest
119+ // client configuration or the phantom subnets that we select from.
120+ func loadConfig (configPath string ) (* config , error ) {
121+ conf := & config {}
122+ _ , err := toml .DecodeFile (configPath , conf )
123+ if err != nil {
124+ return nil , err
125+ }
126+
127+ conf .latestClientConf , err = parseClientConf (conf .ClientConfPath )
128+ if err != nil {
129+ return nil , err
130+ }
131+
132+ return conf , nil
133+ }
134+
111135func main () {
112136 var configPath string
113137
@@ -126,10 +150,9 @@ func main() {
126150
127151 log .SetFormatter (logFormatter )
128152
129- var conf config
130- _ , err := toml .DecodeFile (configPath , & conf )
153+ conf , err := loadConfig (configPath )
131154 if err != nil {
132- log .Fatalf ("Error in reading config file : %v" , err )
155+ log .Fatalf ("error occurred while parsing config : %v" , err )
133156 }
134157
135158 logClientIP , err := strconv .ParseBool (os .Getenv ("LOG_CLIENT_IP" ))
@@ -166,27 +189,50 @@ func main() {
166189 log .Fatal (err )
167190 }
168191
169- latestClientConf , err := parseClientConf (conf .ClientConfPath )
170- if err != nil {
171- log .Fatal (err )
172- }
173-
174192 dnsPrivKey , err := readKey (conf .DNSPrivkeyPath )
175193 if err != nil {
176194 log .Fatal (err )
177195 }
178196
179- dnsRegServer , err := dnsregserver .NewDNSRegServer (conf .Domain , conf .DNSListenAddr , dnsPrivKey , processor , latestClientConf .GetGeneration (), log .WithField ("registrar" , "DNS" ), metrics )
197+ dnsRegServer , err := dnsregserver .NewDNSRegServer (conf .Domain , conf .DNSListenAddr , dnsPrivKey , processor , conf . latestClientConf .GetGeneration (), log .WithField ("registrar" , "DNS" ), metrics )
180198 if err != nil {
181199 log .Fatal (err )
182200 }
183201
184- apiRegServer , err := apiregserver .NewAPIRegServer (conf .APIPort , processor , latestClientConf , log .WithField ("registrar" , "API" ), logClientIP , metrics )
202+ apiRegServer , err := apiregserver .NewAPIRegServer (conf .APIPort , processor , conf . latestClientConf , log .WithField ("registrar" , "API" ), logClientIP , metrics )
185203 if err != nil {
186204 log .Fatal (err )
187205 }
188206
189207 regServers := []regServer {dnsRegServer , apiRegServer }
190208
209+ signalChan := make (chan os.Signal , 1 )
210+
211+ signal .Notify (
212+ signalChan ,
213+ syscall .SIGHUP , // listen for SIGHUP as reload signal
214+ )
215+
216+ // spawn a goroutine to handle os signals continuously
217+ go func () {
218+ for {
219+ sig := <- signalChan
220+
221+ if sig == syscall .SIGHUP {
222+ conf , err = loadConfig (configPath )
223+ if err != nil {
224+ log .Errorf ("error occurred while reloading config -- aborting reload: %v" , err )
225+ } else {
226+ err := processor .ReloadSubnets ()
227+ if err != nil {
228+ log .Errorf ("failed to reload phantom subnets - aborting reload: %v" , err )
229+ }
230+ apiRegServer .NewClientConf (conf .latestClientConf )
231+ dnsRegServer .UpdateLatestCCGen (conf .latestClientConf .GetGeneration ())
232+ }
233+ }
234+ }
235+ }()
236+
191237 run (regServers )
192238}
0 commit comments