diff --git a/.gitignore b/.gitignore index 7e048ddf..23993a7d 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ pause/pause.o /docs/ocid.8 /docs/ocid.conf.5 vendor/src/github.com/kubernetes-incubator/cri-o +ocid.conf diff --git a/Makefile b/Makefile index 6d8e5fc0..1374626e 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,11 @@ ocid: ${OCID_LINK} ocic: ${OCID_LINK} go build -o ocic ./cmd/client/ +ocid.conf: ocid + ./ocid --config="" config --default > ocid.conf + clean: + rm -f ocid.conf rm -f ocic ocid rm -f ${OCID_LINK} rm -f conmon/conmon.o conmon/conmon @@ -83,7 +87,7 @@ docs/%.5: docs/%.5.md docs: $(MANPAGES_MD:%.md=%) -install: +install: ocid.conf install -D -m 755 ocid ${INSTALLDIR}/ocid install -D -m 755 ocic ${INSTALLDIR}/ocic install -D -m 755 conmon/conmon $(PREFIX)/libexec/ocid/conmon @@ -92,8 +96,13 @@ install: install -m 644 $(wildcard docs/*.8.md) $(PREFIX)/share/man/man8 install -d $(PREFIX)/share/man/man5 install -m 644 $(wildcard docs/*.5.md) $(PREFIX)/share/man/man5 + install -D -m 644 ocid.service $(PREFIX)/lib/systemd/system + install -D -m 644 ocid.conf $(DESTDIR)/etc uninstall: + systemctl stop ocid.service + systemctl disable ocid.service + rm -f $(PREFIX)/lib/systemd/system/ocid.service rm -f ${INSTALLDIR}/{ocid,ocic} rm -f $(PREFIX)/libexec/ocid/{conmon,pause} for i in $(wildcard docs/*.8.md); do \ 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 d523d173..8d75e922 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -205,6 +205,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/ocid.service b/ocid.service new file mode 100644 index 00000000..62f78f28 --- /dev/null +++ b/ocid.service @@ -0,0 +1,23 @@ +[Unit] +Description=Open Container Initiative Daemon +Documentation=https://github.com/kubernetes-incubator/cri-o +After=network.target + +[Service] +Type=notify +EnvironmentFile=-/etc/sysconfig/ocid-storage +EnvironmentFile=-/etc/sysconfig/ocid-network +Environment=GOTRACEBACK=crash +ExecStart=/usr/bin/ocid \ + $OCID_STORAGE_OPTIONS \ + $OCID_NETWORK_OPTIONS \ +ExecReload=/bin/kill -s HUP $MAINPID +TasksMax=8192 +LimitNOFILE=1048576 +LimitNPROC=1048576 +LimitCORE=infinity +TimeoutStartSec=0 +Restart=on-abnormal + +[Install] +WantedBy=multi-user.target 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 +}