Add systemd notify support to Agent. Resolves: #7028 (#9802)

Agent systemd notify added similar to Consul. Resolves: #7028
This commit is contained in:
aphorise 2022-08-29 14:18:47 +02:00 committed by GitHub
parent 534142ff46
commit 35650c9b96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

3
changelog/9802.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
agent: Send notifications to systemd on start and stop.
```

View File

@ -16,6 +16,7 @@ import (
"sync" "sync"
"time" "time"
systemd "github.com/coreos/go-systemd/daemon"
log "github.com/hashicorp/go-hclog" log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/gatedwriter" "github.com/hashicorp/go-secure-stdlib/gatedwriter"
"github.com/hashicorp/vault/api" "github.com/hashicorp/vault/api"
@ -802,6 +803,8 @@ func (c *AgentCommand) Run(args []string) int {
select { select {
case <-c.ShutdownCh: case <-c.ShutdownCh:
c.UI.Output("==> Vault agent shutdown triggered") c.UI.Output("==> Vault agent shutdown triggered")
// Notify systemd that the server is shutting down
c.notifySystemd(systemd.SdNotifyStopping)
// Let the lease cache know this is a shutdown; no need to evict // Let the lease cache know this is a shutdown; no need to evict
// everything // everything
if leaseCache != nil { if leaseCache != nil {
@ -809,6 +812,7 @@ func (c *AgentCommand) Run(args []string) int {
} }
return nil return nil
case <-ctx.Done(): case <-ctx.Done():
c.notifySystemd(systemd.SdNotifyStopping)
return nil return nil
case <-winsvc.ShutdownChannel(): case <-winsvc.ShutdownChannel():
return nil return nil
@ -940,6 +944,9 @@ func (c *AgentCommand) Run(args []string) int {
return 1 return 1
} }
// Notify systemd that the server is ready (if applicable)
c.notifySystemd(systemd.SdNotifyReady)
defer func() { defer func() {
if err := c.removePidFile(config.PidFile); err != nil { if err := c.removePidFile(config.PidFile); err != nil {
c.UI.Error(fmt.Sprintf("Error deleting the PID file: %s", err)) c.UI.Error(fmt.Sprintf("Error deleting the PID file: %s", err))
@ -970,6 +977,19 @@ func verifyRequestHeader(handler http.Handler) http.Handler {
}) })
} }
func (c *AgentCommand) notifySystemd(status string) {
sent, err := systemd.SdNotify(false, status)
if err != nil {
c.logger.Error("error notifying systemd", "error", err)
} else {
if sent {
c.logger.Debug("sent systemd notification", "notification", status)
} else {
c.logger.Debug("would have sent systemd notification (systemd not present)", "notification", status)
}
}
}
func (c *AgentCommand) setStringFlag(f *FlagSets, configVal string, fVar *StringVar) { func (c *AgentCommand) setStringFlag(f *FlagSets, configVal string, fVar *StringVar) {
var isFlagSet bool var isFlagSet bool
f.Visit(func(f *flag.Flag) { f.Visit(func(f *flag.Flag) {