-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add systemd notify support to Agent. Resolves: #7028 #9802
Changes from all commits
d0d28b0
2da19a1
f5c6866
234d486
d7559f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:improvement | ||
agent: Send notifications to systemd on start and stop. | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -794,13 +795,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 | ||
|
@@ -928,6 +932,9 @@ func (c *AgentCommand) Run(args []string) int { | |
return 1 | ||
} | ||
|
||
// Notify systemd that the server is ready (if applicable) | ||
c.notifySystemd(systemd.SdNotifyReady) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried adding this a bit later after the run condition which may be more valid to NotifyReady as and when it's actually ready - however since the time it may take for that is not as easily determinable then getting an earlier start notification coupled with any follow-up stop is probably better than just getting NotifyStopping without ever seeing NotifyReady |
||
|
||
defer func() { | ||
if err := c.removePidFile(config.PidFile); err != nil { | ||
c.UI.Error(fmt.Sprintf("Error deleting the PID file: %s", err)) | ||
|
@@ -958,6 +965,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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we also do this in the case above, when we were asked to shutdown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @ncabatoff I'm not comprehending - does
systemd.SdNotifyStopping
need to be included in any other case other than these two places?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If any one of the two signals are the only mode of exit (outside Windows) then I think these two later here would suffice and void any repeats - but I'm not entirely sure TBH.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's simpler just to add the notifySystemd call to the end of the func, or to a defer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any chance that an end or defer function may take longer or not ever reached? - just thinking aloud in case of processor monitors or other cases an agent may be forced quit and never actually ending.
Anyway @ncabatoff any chance you can do a PR if you think a defer may be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defer will always fire when the func returns, and it shouldn't be any slower than doing it just before a return statement.