From 2feed18b2838719bcd730b3dc8c2fb02831c424e Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 31 May 2022 13:02:23 +0200 Subject: [PATCH] Support reloading ACLs with SIGHUP Also continously listen for signals, not just once. --- app.go | 59 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/app.go b/app.go index cc867a40..054fd178 100644 --- a/app.go +++ b/app.go @@ -728,35 +728,48 @@ func (h *Headscale) Serve() error { syscall.SIGHUP) go func(c chan os.Signal) { // Wait for a SIGINT or SIGKILL: - sig := <-c - switch sig { - case syscall.SIGHUP: - log.Info(). - Str("signal", sig.String()). - Msg("Received SIGHUP, reloading ACL and Config") + for { + sig := <-c + switch sig { + case syscall.SIGHUP: + log.Info(). + Str("signal", sig.String()). + Msg("Received SIGHUP, reloading ACL and Config") - // TODO(kradalby): Reload config on SIGHUP + // TODO(kradalby): Reload config on SIGHUP - default: - log.Info(). - Str("signal", sig.String()). - Msg("Received signal to stop, shutting down gracefully") + if h.cfg.ACL.PolicyPath != "" { + aclPath := AbsolutePathFromConfigPath(h.cfg.ACL.PolicyPath) + err := h.LoadACLPolicy(aclPath) + if err != nil { + log.Error().Err(err).Msg("Failed to reload ACL policy") + } + log.Info(). + Str("path", aclPath). + Msg("ACL policy successfully reloaded") + } - // Gracefully shut down servers - promHTTPServer.Shutdown(ctx) - httpServer.Shutdown(ctx) - grpcSocket.GracefulStop() + default: + log.Info(). + Str("signal", sig.String()). + Msg("Received signal to stop, shutting down gracefully") - // Close network listeners - promHTTPListener.Close() - httpListener.Close() - grpcGatewayConn.Close() + // Gracefully shut down servers + promHTTPServer.Shutdown(ctx) + httpServer.Shutdown(ctx) + grpcSocket.GracefulStop() - // Stop listening (and unlink the socket if unix type): - socketListener.Close() + // Close network listeners + promHTTPListener.Close() + httpListener.Close() + grpcGatewayConn.Close() - // And we're done: - os.Exit(0) + // Stop listening (and unlink the socket if unix type): + socketListener.Close() + + // And we're done: + os.Exit(0) + } } }(sigc)