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

Attempt to notify systemd of service readiness on linux #929

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions cmd/nebula/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func main() {

if !*configTest {
ctrl.Start()
notifyReady(l)
ctrl.ShutdownBlock()
}

Expand Down
42 changes: 42 additions & 0 deletions cmd/nebula/notify_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"net"
"os"
"time"

"github.com/sirupsen/logrus"
)

// SdNotifyReady tells systemd the service is ready and dependent services can now be started
// https://www.freedesktop.org/software/systemd/man/sd_notify.html
// https://www.freedesktop.org/software/systemd/man/systemd.service.html
const SdNotifyReady = "READY=1"

func notifyReady(l *logrus.Logger) {
sockName := os.Getenv("NOTIFY_SOCKET")
if sockName == "" {
l.Debugln("NOTIFY_SOCKET systemd env var not set, not sending ready signal")
return
}

conn, err := net.DialTimeout("unixgram", sockName, time.Second)
if err != nil {
l.WithError(err).Debugln("failed to connect to systemd notification socket")
return
}
defer conn.Close()

err = conn.SetWriteDeadline(time.Now().Add(time.Second))
if err != nil {
l.WithError(err).Debugln("failed to set the write deadline for the systemd notification socket")
return
}

if _, err = conn.Write([]byte(SdNotifyReady)); err != nil {
l.WithError(err).Debugln("failed to signal the systemd notification socket")
return
}

l.Debugln("notified systemd the service is ready")
}
10 changes: 10 additions & 0 deletions cmd/nebula/notify_notlinux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !linux
// +build !linux

package main

import "github.com/sirupsen/logrus"

func notifyReady(_ *logrus.Logger) {
// No init service to notify
}
2 changes: 2 additions & 0 deletions dist/arch/nebula.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Wants=basic.target network-online.target nss-lookup.target time-sync.target
After=basic.target network.target network-online.target

[Service]
Type=notify
NotifyAccess=main
SyslogIdentifier=nebula
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/bin/nebula -config /etc/nebula/config.yml
Expand Down
2 changes: 2 additions & 0 deletions dist/fedora/nebula.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ After=basic.target network.target network-online.target
Before=sshd.service

[Service]
Type=notify
NotifyAccess=main
SyslogIdentifier=nebula
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/bin/nebula -config /etc/nebula/config.yml
Expand Down
2 changes: 2 additions & 0 deletions examples/service_scripts/nebula.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ After=basic.target network.target network-online.target
Before=sshd.service

[Service]
Type=notify
NotifyAccess=main
SyslogIdentifier=nebula
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/bin/nebula -config /etc/nebula/config.yml
Expand Down