From eca7ac9f0d4096e86131ff0f068862320af5eb82 Mon Sep 17 00:00:00 2001 From: Ekaterina Lebedeva Date: Tue, 21 Nov 2023 12:08:49 +0300 Subject: [PATCH] [#552] Add sdnotify package To avoid using third-party dependencies. Signed-off-by: Ekaterina Lebedeva --- cmd/frostfs-node/control.go | 7 ++-- go.mod | 1 - go.sum | Bin 46999 -> 45864 bytes pkg/util/sdnotify/sdnotify.go | 59 ++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 pkg/util/sdnotify/sdnotify.go diff --git a/cmd/frostfs-node/control.go b/cmd/frostfs-node/control.go index fcd84db3c..6b603d433 100644 --- a/cmd/frostfs-node/control.go +++ b/cmd/frostfs-node/control.go @@ -10,8 +10,8 @@ import ( "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control" controlSvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/server" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/tree" + "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sdnotify" cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id" - sysdnotify "github.com/iguanesolutions/go-systemd/v5/notify" "go.uber.org/zap" "google.golang.org/grpc" ) @@ -109,14 +109,13 @@ func (c *cfg) HealthStatus() control.HealthStatus { return control.HealthStatus(c.healthStatus.Load()) } -func (c *cfg) notifySystemd(st control.HealthStatus) string { +func (c *cfg) notifySystemd(st control.HealthStatus) { status := fmt.Sprintf("%v, %v", st.Number(), st) - err := sysdnotify.Status(status) + err := sdnotify.Status(status) if err == nil { c.log.Info(fmt.Sprintf("reported STATUS=\"%v\" to systemd", status)) } if err != nil { c.log.Error(logs.FailedToReportStatusToSystemd, zap.String("error", err.Error())) } - return fmt.Sprintf("%v, %v", st, control.HealthStatus_name[int32(st)]) } diff --git a/go.mod b/go.mod index 3037a6167..3e79f7ff7 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 github.com/google/uuid v1.4.0 github.com/hashicorp/golang-lru/v2 v2.0.7 - github.com/iguanesolutions/go-systemd/v5 v5.1.1 github.com/klauspost/compress v1.17.4 github.com/mitchellh/go-homedir v1.1.0 github.com/mr-tron/base58 v1.2.0 diff --git a/go.sum b/go.sum index 780bdbcdbd1cc9f32d367400a9efc03fb377e0c2..15ae22422585062b9c83d02a3b832152a1cfb18f 100644 GIT binary patch delta 14 WcmbRKo@vE1rVW-Bn^)MpuL1xzTL(G- delta 990 zcmb`_J93*)6b9gl(`6M(8;#*!-ee{PMhKxNvJ8^k@w|Y6D*+b?H!MO$wvaM2NtyMe z^%fGRi7bx_O|pRVo&WrEzWluU{B-qImJUC}_bm%}hJzBvF#L7kxE_#Y7Xrtn5vDTi zcOv)^?Zsk;?Z^v>J<@s7IP#!m7yCrij5_sg94br0s&q_tpWdOkiik^jIHO}koIEIT zN~##=Z%&{Vwvx7JWuO^pb31?(-63zhMRnQ?ga1|7Q6 z2N_VuMVkBV#t7;dH5#Lfgq^O2#vP+fn~IoyICF{_*He0#$mY`X^jko|!*{9oO_rV> Fe*t3SOf>)i diff --git a/pkg/util/sdnotify/sdnotify.go b/pkg/util/sdnotify/sdnotify.go new file mode 100644 index 000000000..7c3d2f1d2 --- /dev/null +++ b/pkg/util/sdnotify/sdnotify.go @@ -0,0 +1,59 @@ +package sdnotify + +import ( + "fmt" + "net" + "os" + "strings" +) + +const ( + ReadyEnabled = "READY=1" + StoppingEnabled = "STOPPING=1" + ReloadingEnabled = "RELOADING=1" +) + +var socket *net.UnixAddr + +// Initializes socket with provided name of +// environment variable. +func InitSocket() error { + notifySocket := os.Getenv("NOTIFY_SOCKET") + if notifySocket == "" { + return fmt.Errorf("\"NOTIFY_SOCKET\" environment variable is not present") + } + socket = &net.UnixAddr{ + Name: notifySocket, + Net: "unixgram", + } + return nil +} + +// FlagAndStatus sends systemd a combination of a +// well-known status and STATUS=%s{status}, separated by newline. +func FlagAndStatus(status string) error { + status += "\nSTATUS=" + strings.TrimSuffix(status, "=1") + return Send(status) +} + +// Status sends systemd notify STATUS=%s{status}. +func Status(status string) error { + return Send(fmt.Sprintf("STATUS=%s", status)) +} + +// Send state through the notify socket if any. +// If the notify socket was not detected, it returns an error. +func Send(state string) error { + if socket == nil { + return fmt.Errorf("socket is not initialized") + } + conn, err := net.DialUnix(socket.Net, nil, socket) + if err != nil { + return fmt.Errorf("can't open unix socket: %v", err) + } + defer conn.Close() + if _, err = conn.Write([]byte(state)); err != nil { + return fmt.Errorf("can't write into the unix socket: %v", err) + } + return nil +}