Skip to content

Commit

Permalink
Add systemd notify support to Agent. Resolves: #7028 (#9802)
Browse files Browse the repository at this point in the history
Agent systemd notify added similar to Consul. Resolves: #7028
  • Loading branch information
aphorise committed Aug 29, 2022
1 parent 24bb6c2 commit 52493e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelog/9802.txt
@@ -0,0 +1,3 @@
```release-note:improvement
agent: Send notifications to systemd on start and stop.
```
20 changes: 20 additions & 0 deletions command/agent.go
Expand Up @@ -16,6 +16,7 @@ import (
"sync"
"time"

systemd "github.com/coreos/go-systemd/daemon"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/gatedwriter"
"github.com/hashicorp/vault/api"
Expand Down Expand Up @@ -802,13 +803,16 @@ func (c *AgentCommand) Run(args []string) int {
select {
case <-c.ShutdownCh:
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
// everything
if leaseCache != nil {
leaseCache.SetShuttingDown(true)
}
return nil
case <-ctx.Done():
c.notifySystemd(systemd.SdNotifyStopping)
return nil
case <-winsvc.ShutdownChannel():
return nil
Expand Down Expand Up @@ -940,6 +944,9 @@ func (c *AgentCommand) Run(args []string) int {
return 1
}

// Notify systemd that the server is ready (if applicable)
c.notifySystemd(systemd.SdNotifyReady)

defer func() {
if err := c.removePidFile(config.PidFile); err != nil {
c.UI.Error(fmt.Sprintf("Error deleting the PID file: %s", err))
Expand Down Expand Up @@ -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) {
var isFlagSet bool
f.Visit(func(f *flag.Flag) {
Expand Down

0 comments on commit 52493e3

Please sign in to comment.