From d0d28b0899758cde60a35c8fb06bf66c3576e54b Mon Sep 17 00:00:00 2001 From: Mehdi Ahmadi Date: Sun, 23 Aug 2020 21:29:44 +0200 Subject: [PATCH 1/2] Server systemd notify added similar to Consul. Resolves: #7028 --- command/server.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/command/server.go b/command/server.go index ac20c263ed03b..f4d996f7affb3 100644 --- a/command/server.go +++ b/command/server.go @@ -20,6 +20,7 @@ import ( "sync" "time" + "github.com/hashicorp/consul/agent/systemd" "github.com/hashicorp/errwrap" "github.com/hashicorp/go-hclog" log "github.com/hashicorp/go-hclog" @@ -73,9 +74,17 @@ const ( storageTypeConsul = "consul" ) +// notifier is called after a successful JoinLAN. +type notifier interface { + Notify(string) error +} + type ServerCommand struct { *BaseCommand + // joinLANNotifier is called after a successful JoinLAN. + NotifierSystemd notifier + AuditBackends map[string]audit.Factory CredentialBackends map[string]logical.Factory LogicalBackends map[string]logical.Factory @@ -798,6 +807,9 @@ func (c *ServerCommand) processLogLevelAndFormat(config *server.Config) (log.Lev } func (c *ServerCommand) Run(args []string) int { + + c.NotifierSystemd = &systemd.Notifier{} + f := c.Flags() if err := f.Parse(args); err != nil { @@ -1548,6 +1560,9 @@ CLUSTER_SYNTHESIS_COMPLETE: for { err := core.UnsealWithStoredKeys(context.Background()) if err == nil { + if notifyErr := c.NotifierSystemd.Notify(systemd.Ready); notifyErr != nil { + c.logger.Debug("server: systemd notify failed", notifyErr) + } return } From 2da19a1722d0cb548735384cfd97dd86a120e499 Mon Sep 17 00:00:00 2001 From: Mehdi Ahmadi Date: Sun, 23 Aug 2020 22:09:51 +0200 Subject: [PATCH 2/2] Server systemd notify added similar to Consul for Vault agent & server. Resolves: #7028 --- command/agent.go | 9 +++++++++ command/server.go | 8 +++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/command/agent.go b/command/agent.go index 018d01e41243b..0ad397df7ddf9 100644 --- a/command/agent.go +++ b/command/agent.go @@ -15,6 +15,7 @@ import ( "sync" "time" + "github.com/hashicorp/consul/agent/systemd" "github.com/hashicorp/errwrap" log "github.com/hashicorp/go-hclog" "github.com/hashicorp/vault/api" @@ -51,6 +52,9 @@ var _ cli.CommandAutocomplete = (*AgentCommand)(nil) type AgentCommand struct { *BaseCommand + // NotifierSystemd is called after Vault starts. + NotifierSystemd notifier + ShutdownCh chan struct{} SighupCh chan struct{} @@ -164,6 +168,7 @@ func (c *AgentCommand) AutocompleteFlags() complete.Flags { func (c *AgentCommand) Run(args []string) int { f := c.Flags() + c.NotifierSystemd = &systemd.Notifier{} if err := f.Parse(args); err != nil { c.UI.Error(err.Error()) @@ -585,6 +590,10 @@ func (c *AgentCommand) Run(args []string) int { if err := c.storePidFile(config.PidFile); err != nil { c.UI.Error(fmt.Sprintf("Error storing PID: %s", err)) return 1 + } else { + if notifyErr := c.NotifierSystemd.Notify(systemd.Ready); notifyErr != nil { + c.logger.Debug("server: systemd notify failed", notifyErr) + } } defer func() { diff --git a/command/server.go b/command/server.go index f4d996f7affb3..ede61d28a43f2 100644 --- a/command/server.go +++ b/command/server.go @@ -74,7 +74,7 @@ const ( storageTypeConsul = "consul" ) -// notifier is called after a successful JoinLAN. +// notifier is called after a successful Vault start. type notifier interface { Notify(string) error } @@ -82,7 +82,7 @@ type notifier interface { type ServerCommand struct { *BaseCommand - // joinLANNotifier is called after a successful JoinLAN. + // NotifierSystemd is called after Vault starts. NotifierSystemd notifier AuditBackends map[string]audit.Factory @@ -807,10 +807,8 @@ func (c *ServerCommand) processLogLevelAndFormat(config *server.Config) (log.Lev } func (c *ServerCommand) Run(args []string) int { - - c.NotifierSystemd = &systemd.Notifier{} - f := c.Flags() + c.NotifierSystemd = &systemd.Notifier{} if err := f.Parse(args); err != nil { c.UI.Error(err.Error())