Skip to content
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

systemd-notify-all option to notify systemd after role certificates #2599

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions libs/go/sia/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,7 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
}
}

if cmd == "systemd-notify" {
err = util.NotifySystemdReady()
if err != nil {
log.Printf("failed to notify systemd: %v", err)
}
}

util.NotifySystemdReadyForCommand(cmd, "systemd-notify")
log.Printf("Identity established for services: %s\n", svcs)

stop := make(chan bool, 1)
Expand Down Expand Up @@ -756,6 +750,7 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
}
GetRoleCertificates(ztsUrl, opts)
util.ExecuteScriptWithoutBlock(opts.RunAfterParts, opts.RunAfterFailExit)
util.NotifySystemdReadyForCommand(cmd, "systemd-notify-all")

if opts.SDSUdsPath != "" {
certUpdates <- true
Expand Down
9 changes: 2 additions & 7 deletions libs/go/sia/aws/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,7 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
}
}

if cmd == "systemd-notify" {
err = util.NotifySystemdReady()
if err != nil {
log.Printf("failed to notify systemd: %v", err)
}
}

util.NotifySystemdReadyForCommand(cmd, "systemd-notify")
log.Printf("Identity established for services: %s\n", svcs)

stop := make(chan bool, 1)
Expand Down Expand Up @@ -757,6 +751,7 @@ func RunAgent(siaCmd, ztsUrl string, opts *options.Options) {
}
GetRoleCertificates(ztsUrl, opts)
util.ExecuteScriptWithoutBlock(opts.RunAfterParts, opts.RunAfterFailExit)
util.NotifySystemdReadyForCommand(cmd, "systemd-notify-all")

if opts.SDSUdsPath != "" {
certUpdates <- true
Expand Down
17 changes: 16 additions & 1 deletion libs/go/sia/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,22 @@ func ParseSiaCmd(siaCmd string) (string, bool) {
}
}

// NotifySystemDReady sends a notification to systemd that the service is ready
// NotifySystemdReadyForCommand sends a notification to systemd that the
// service is ready if the clientCmd argument matches to the notifyCmd
func NotifySystemdReadyForCommand(clientCmd, notifyCmd string) error {
// if our client command is not the requested value
// then we're going to skip the notification
if clientCmd != notifyCmd {
return nil
}
err := NotifySystemdReady()
if err != nil {
log.Printf("failed to notify systemd: %v", err)
}
return err
}

// NotifySystemdReady sends a notification to systemd that the service is ready
func NotifySystemdReady() error {
notifySocket := os.Getenv("NOTIFY_SOCKET")
if notifySocket == "" {
Expand Down
14 changes: 14 additions & 0 deletions libs/go/sia/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1793,4 +1793,18 @@ func TestNotifySystemdReady(test *testing.T) {
defer os.Remove(notifySocket)
err = NotifySystemdReady()
assert.Nil(test, err)
os.Unsetenv("NOTIFY_SOCKET")
}

func TestNotifySystemdReadyForCommand(test *testing.T) {

// when commands don't match there is always nil error
err := NotifySystemdReadyForCommand("systemd-notify", "init")
assert.Nil(test, err)

// when commands match, the code is executed, so we should
// get an error that notify socket is not set
err = NotifySystemdReadyForCommand("systemd-notify", "systemd-notify")
assert.NotNil(test, err)
assert.Equal(test, err.Error(), "notify socket is not set")
}