Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
cmd: Implement sd_notify() to notify systemd about readiness (#3963)
Issue: #3786 Based on Gaurav Dhameeja's work in #3908.
Stefan Tatschner committed
Jan 12, 2021 at 22:38 UTC
59071ea15d2aacb69fcfc088f4996717cd2bfc73
4 files changed
+129
cmd/commandfuncs.go
+13
@@ -271,6 +271,10 @@ func cmdRun(fl Flags) (int, error) {
271
}
272
}
273
274
+ if err := NotifyReadiness(); err != nil {
275
+ caddy.Log().Error("unable to notify readiness to service manager", zap.Error(err))
276
+ }
277
+
278
select {}
279
}
280
@@ -291,6 +295,15 @@ func cmdReload(fl Flags) (int, error) {
295
reloadCmdConfigAdapterFlag := fl.String("adapter")
296
reloadCmdAddrFlag := fl.String("address")
297
298
+ if err := NotifyReloading(); err != nil {
299
+ caddy.Log().Error("unable to notify reloading to service manager", zap.Error(err))
300
+ }
301
+ defer func() {
302
+ if err := NotifyReadiness(); err != nil {
303
+ caddy.Log().Error("unable to notify readiness to service manager", zap.Error(err))
304
+ }
305
+ }()
306
+
307
// get the config in caddy's native format
308
config, configFile, err := loadConfig(reloadCmdConfigFlag, reloadCmdConfigAdapterFlag)
309
if err != nil {
cmd/notify.go
new
+23
@@ -0,0 +1,23 @@
1
+// Copyright 2015 Matthew Holt and The Caddy Authors
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+// http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+package caddycmd
16
+
17
+func NotifyReadiness() error {
18
+ return notifyReadiness()
19
+}
20
+
21
+func NotifyReloading() error {
22
+ return notifyReloading()
23
+}
cmd/notify_linux.go
new
+68
@@ -0,0 +1,68 @@
1
+// Copyright 2015 Matthew Holt and The Caddy Authors
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+// http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+package caddycmd
16
+
17
+import (
18
+ "io"
19
+ "net"
20
+ "os"
21
+ "strings"
22
+)
23
+
24
+// The documentation about this IPC protocol is available here:
25
+// https://www.freedesktop.org/software/systemd/man/sd_notify.html
26
+
27
+func sdNotify(path, payload string) error {
28
+ socketAddr := &net.UnixAddr{
29
+ Name: path,
30
+ Net: "unixgram",
31
+ }
32
+
33
+ conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
34
+ if err != nil {
35
+ return err
36
+ }
37
+ defer conn.Close()
38
+
39
+ if _, err := io.Copy(conn, strings.NewReader(payload)); err != nil {
40
+ return err
41
+ }
42
+ return nil
43
+}
44
+
45
+// notifyReadiness notifies systemd caddy that has finished its
46
+// initialization routines.
47
+func notifyReadiness() error {
48
+ val, ok := os.LookupEnv("NOTIFY_SOCKET")
49
+ if !ok || val == "" {
50
+ return nil
51
+ }
52
+ if err := sdNotify(val, "READY=1"); err != nil {
53
+ return err
54
+ }
55
+ return nil
56
+}
57
+
58
+// notifyReadiness notifies systemd that caddy is reloading its config.
59
+func notifyReloading() error {
60
+ val, ok := os.LookupEnv("NOTIFY_SOCKET")
61
+ if !ok || val == "" {
62
+ return nil
63
+ }
64
+ if err := sdNotify(val, "RELOADING=1"); err != nil {
65
+ return err
66
+ }
67
+ return nil
68
+}
cmd/notify_other.go
new
+25
@@ -0,0 +1,25 @@
1
+// Copyright 2015 Matthew Holt and The Caddy Authors
2
+//
3
+// Licensed under the Apache License, Version 2.0 (the "License");
4
+// you may not use this file except in compliance with the License.
5
+// You may obtain a copy of the License at
6
+//
7
+// http://www.apache.org/licenses/LICENSE-2.0
8
+//
9
+// Unless required by applicable law or agreed to in writing, software
10
+// distributed under the License is distributed on an "AS IS" BASIS,
11
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+// See the License for the specific language governing permissions and
13
+// limitations under the License.
14
+
15
+// +build !linux
16
+
17
+package caddycmd
18
+
19
+func notifyReadiness() error {
20
+ return nil
21
+}
22
+
23
+func notifyReloading() error {
24
+ return nil
25
+}