From 09bed250746c353b5c56a969ee840e70175ac92d Mon Sep 17 00:00:00 2001 From: Dan Walsh Date: Wed, 12 Oct 2016 16:27:28 -0400 Subject: [PATCH] Add support for sd_notify Signed-off-by: Dan Walsh --- cmd/server/daemon_linux.go | 20 ++++++++++++ cmd/server/main.go | 4 +++ hack/vendor.sh | 1 + .../coreos/go-systemd/daemon/sdnotify.go | 31 +++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 cmd/server/daemon_linux.go create mode 100644 vendor/src/github.com/coreos/go-systemd/daemon/sdnotify.go diff --git a/cmd/server/daemon_linux.go b/cmd/server/daemon_linux.go new file mode 100644 index 00000000..43b6ee24 --- /dev/null +++ b/cmd/server/daemon_linux.go @@ -0,0 +1,20 @@ +// +build linux + +package main + +import ( + "github.com/Sirupsen/logrus" + systemdDaemon "github.com/coreos/go-systemd/daemon" +) + +func sdNotify() { + if err := systemdDaemon.SdNotify("READY=1"); err != nil { + logrus.Warnf("Failed to sd_notify systemd: %v", err) + } +} + +// notifySystem sends a message to the host when the server is ready to be used +func notifySystem() { + // Tell the init daemon we are accepting requests + go sdNotify() +} diff --git a/cmd/server/main.go b/cmd/server/main.go index 41f35814..49e19ed2 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -188,6 +188,10 @@ func main() { runtime.RegisterRuntimeServiceServer(s, service) runtime.RegisterImageServiceServer(s, service) + + // after the daemon is done setting up we can notify systemd api + notifySystem() + if err := s.Serve(lis); err != nil { logrus.Fatal(err) } diff --git a/hack/vendor.sh b/hack/vendor.sh index 5f0d6bfd..4363504a 100755 --- a/hack/vendor.sh +++ b/hack/vendor.sh @@ -46,6 +46,7 @@ case $# in ;; esac +clone git github.com/coreos/go-systemd/daemon clone git github.com/BurntSushi/toml v0.2.0 clone git github.com/Sirupsen/logrus v0.10.0 clone git github.com/containers/image f6f11ab5cf8b1e70ef4aa3f8b6fdb4b671d16abd diff --git a/vendor/src/github.com/coreos/go-systemd/daemon/sdnotify.go b/vendor/src/github.com/coreos/go-systemd/daemon/sdnotify.go new file mode 100644 index 00000000..b92b1911 --- /dev/null +++ b/vendor/src/github.com/coreos/go-systemd/daemon/sdnotify.go @@ -0,0 +1,31 @@ +// Code forked from Docker project +package daemon + +import ( + "errors" + "net" + "os" +) + +var SdNotifyNoSocket = errors.New("No socket") + +// SdNotify sends a message to the init daemon. It is common to ignore the error. +func SdNotify(state string) error { + socketAddr := &net.UnixAddr{ + Name: os.Getenv("NOTIFY_SOCKET"), + Net: "unixgram", + } + + if socketAddr.Name == "" { + return SdNotifyNoSocket + } + + conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr) + if err != nil { + return err + } + defer conn.Close() + + _, err = conn.Write([]byte(state)) + return err +}