forked from TrueCloudLab/frostfs-node
[#552] Add sdnotify package
To avoid using third-party dependencies. Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
This commit is contained in:
parent
9b2dce5763
commit
eca7ac9f0d
4 changed files with 62 additions and 17 deletions
59
pkg/util/sdnotify/sdnotify.go
Normal file
59
pkg/util/sdnotify/sdnotify.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue