From 4f36c1da44d8ae09fdeddbef0f9d4c8f8c724375 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 30 Apr 2024 02:38:08 +0300 Subject: [PATCH 01/11] [initsystem/sdnotify] Add new package for sending messages to systemd --- CHANGELOG.md | 4 ++ ek.go | 2 +- initsystem/sdnotify/example_test.go | 19 ++++++ initsystem/sdnotify/sdnotify_linux.go | 89 +++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 initsystem/sdnotify/example_test.go create mode 100644 initsystem/sdnotify/sdnotify_linux.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b928b9f..b6e353f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Changelog +### 12.121.0 + +- `[initsystem/sdnotify]` Added new package for sending messages to systemd + ### 12.120.0 - `[knf]` Added methods `Alias` and `Config.Alias` diff --git a/ek.go b/ek.go index b217a9ba..11292799 100644 --- a/ek.go +++ b/ek.go @@ -21,7 +21,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.120.0" +const VERSION = "12.121.0" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/initsystem/sdnotify/example_test.go b/initsystem/sdnotify/example_test.go new file mode 100644 index 00000000..36a94d13 --- /dev/null +++ b/initsystem/sdnotify/example_test.go @@ -0,0 +1,19 @@ +package sdnotify + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +func ExampleConnect() { + err := Connect() + + if err != nil { + panic(err.Error()) + } + + Status("Loading data %d%%", 50) + Ready() +} diff --git a/initsystem/sdnotify/sdnotify_linux.go b/initsystem/sdnotify/sdnotify_linux.go new file mode 100644 index 00000000..03214935 --- /dev/null +++ b/initsystem/sdnotify/sdnotify_linux.go @@ -0,0 +1,89 @@ +// Package sdnotify provides methods for sending notifications to systemd +package sdnotify + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "fmt" + "net" + "os" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +var ( + ErrNoSocket = fmt.Errorf("NOTIFY_SOCKET is empty") + ErrNotConnected = fmt.Errorf("Not connected to socket") +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +var conn net.Conn + +// ////////////////////////////////////////////////////////////////////////////////// // + +// Connect connects systemd to socket +func Connect() error { + var err error + + socket := os.Getenv("NOTIFY_SOCKET") + + if err != nil { + return ErrNoSocket + } + + conn, err = net.Dial("unixgram", socket) + + if err != nil { + return fmt.Errorf("Can't connect to socket: %w", err) + } + + return nil +} + +// Notify sends provided message to systemd +func Notify(msg string) error { + if conn == nil { + return ErrNotConnected + } + + _, err := fmt.Fprint(conn, msg) + + return err +} + +// Ready sends READY message to systemd +func Ready() error { + return Notify("READY=1") +} + +// Reloading sends RELOADING message to systemd +func Reloading() error { + return Notify("RELOADING=1") +} + +// Stopping sends STOPPING message to systemd +func Stopping() error { + return Notify("STOPPING=1") +} + +// MainPID sends MAINPID message with PID to systemd +func MainPID(pid int) error { + return Notify(fmt.Sprintf("MAINPID=%d", pid)) +} + +// ExtendTimeout sends EXTEND_TIMEOUT_USEC message to systemd +func ExtendTimeout(sec float64) error { + usec := uint(sec * 1_000_000) + return Notify(fmt.Sprintf("EXTEND_TIMEOUT_USEC=%d", usec)) +} + +// Status sends status message to systemd +func Status(format string, a ...any) error { + return Notify(fmt.Sprintf(format, a...)) +} From 213b253c18ee1535063f3a0c8b82adabddffd45e Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 30 Apr 2024 02:39:46 +0300 Subject: [PATCH 02/11] Update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ba807315..e0dff8b9 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ go get -u github.com/essentialkaos/ek/v12 * [`hash`](https://kaos.sh/g/ek.v12/hash) — Package hash contains different hash algorithms and utilities * [`httputil`](https://kaos.sh/g/ek.v12/httputil) — Package provides methods for working with HTTP request/responses * [`initsystem`](https://kaos.sh/g/ek.v12/initsystem) — Package provides methods for working with different init systems (sysv, upstart, systemd) +* [`initsystem`](https://kaos.sh/g/ek.v12/initsystem/sdnotify) — Package provides methods methods for sending [notifications to systemd](https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#Well-known%20assignments) * [`jsonutil`](https://kaos.sh/g/ek.v12/jsonutil) — Package provides methods for working with JSON data * [`knf`](https://kaos.sh/g/ek.v12/knf) — Package provides methods for working with configuration files in [KNF format](https://kaos.sh/knf-spec) * [`knf/united`](https://kaos.sh/g/ek.v12/knf/united) — Package provides united configuration (_knf + options + environment variables_) From 2034b9261aa957cfc271a79da5af7bcea32f469f Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 30 Apr 2024 02:42:33 +0300 Subject: [PATCH 03/11] Update tests --- .scripts/packages.list | 1 + 1 file changed, 1 insertion(+) diff --git a/.scripts/packages.list b/.scripts/packages.list index d2043fcd..b59a158c 100644 --- a/.scripts/packages.list +++ b/.scripts/packages.list @@ -18,6 +18,7 @@ * + hash * + httputil * - initsystem +* ! initsystem/sdnotify * + jsonutil * + knf * + knf/united From 1a24defba8f3446448f91b4cea077a0d9fe90f0a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 30 Apr 2024 11:32:09 +0300 Subject: [PATCH 04/11] [initsystem/sdnotify] Add stubs for Windows and macOS --- initsystem/sdnotify/sdnotify_stubs.go | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 initsystem/sdnotify/sdnotify_stubs.go diff --git a/initsystem/sdnotify/sdnotify_stubs.go b/initsystem/sdnotify/sdnotify_stubs.go new file mode 100644 index 00000000..a9bfce4a --- /dev/null +++ b/initsystem/sdnotify/sdnotify_stubs.go @@ -0,0 +1,54 @@ +//go:build windows || darwin +// +build windows darwin + +// Package sdnotify provides methods for sending notifications to systemd +package sdnotify + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +// ////////////////////////////////////////////////////////////////////////////////// // + +// ❗ Connect connects systemd to socket +func Connect() error { + panic("UNSUPPORTED") +} + +// ❗ Notify sends provided message to systemd +func Notify(msg string) error { + panic("UNSUPPORTED") +} + +// ❗ Ready sends READY message to systemd +func Ready() error { + panic("UNSUPPORTED") +} + +// ❗ Reloading sends RELOADING message to systemd +func Reloading() error { + panic("UNSUPPORTED") +} + +// ❗ Stopping sends STOPPING message to systemd +func Stopping() error { + panic("UNSUPPORTED") +} + +// ❗ MainPID sends MAINPID message with PID to systemd +func MainPID(pid int) error { + panic("UNSUPPORTED") +} + +// ❗ ExtendTimeout sends EXTEND_TIMEOUT_USEC message to systemd +func ExtendTimeout(sec float64) error { + panic("UNSUPPORTED") +} + +// ❗ Status sends status message to systemd +func Status(format string, a ...any) error { + panic("UNSUPPORTED") +} From cc679b4912222b41eae4d69c93f321dc00cd8bba Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 1 May 2024 02:08:40 +0000 Subject: [PATCH 05/11] Bump github.com/essentialkaos/depsy from 1.1.0 to 1.2.0 Bumps [github.com/essentialkaos/depsy](https://github.com/essentialkaos/depsy) from 1.1.0 to 1.2.0. - [Release notes](https://github.com/essentialkaos/depsy/releases) - [Commits](https://github.com/essentialkaos/depsy/compare/v1.1.0...v1.2.0) --- updated-dependencies: - dependency-name: github.com/essentialkaos/depsy dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index a06b6bd7..d89b0c61 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/essentialkaos/check v1.4.0 - github.com/essentialkaos/depsy v1.1.0 + github.com/essentialkaos/depsy v1.2.0 github.com/essentialkaos/go-linenoise/v3 v3.6.0 golang.org/x/crypto v0.22.0 golang.org/x/sys v0.19.0 diff --git a/go.sum b/go.sum index 05d7b082..01561566 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk= github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0= -github.com/essentialkaos/depsy v1.1.0 h1:U6dp687UkQwXlZU17Hg2KMxbp3nfZAoZ8duaeUFYvJI= -github.com/essentialkaos/depsy v1.1.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8= +github.com/essentialkaos/depsy v1.2.0 h1:vPuzyS/To7OEurdTDns23SMW0/A1Dw74wuV14YjmH6k= +github.com/essentialkaos/depsy v1.2.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8= github.com/essentialkaos/go-linenoise/v3 v3.6.0 h1:deLcrodtLIkcHjNyW/MoQpjznXPVqvwlspxk7s/5YeY= github.com/essentialkaos/go-linenoise/v3 v3.6.0/go.mod h1:Fi6kLdZdURkXHpRkIiX2nFGORNv81CXTZ2Mn72i/cn0= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= From 87a9bba097c46584f29d788d358e23181cef2536 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 1 May 2024 14:47:47 +0300 Subject: [PATCH 06/11] [support/deps] Add support for local path replacements --- CHANGELOG.md | 1 + support/support_nix.go | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6e353f4..93d6015a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 12.121.0 - `[initsystem/sdnotify]` Added new package for sending messages to systemd +- `[support/deps]` Added support for local path replacements ### 12.120.0 diff --git a/support/support_nix.go b/support/support_nix.go index 82b3eada..8e974d5d 100644 --- a/support/support_nix.go +++ b/support/support_nix.go @@ -460,9 +460,16 @@ func (i *Info) printDependencies() { fmtutil.Separator(false, "DEPENDENCIES") for _, dep := range i.Deps { - if dep.Extra == "" { + extra := dep.Extra + + if strutil.HasPrefixAny(extra, ".", "/") { + extra = "local-path: " + extra + } + + switch dep.Extra { + case "": fmtc.Printf(" {s}%8s{!} %s\n", dep.Version, dep.Path) - } else { + default: fmtc.Printf(" {s}%8s{!} %s {s-}(%s){!}\n", dep.Version, dep.Path, dep.Extra) } } From a04af4792fc76b06e7658ceea746f31591347712 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 1 May 2024 23:57:12 +0300 Subject: [PATCH 07/11] Dependencies update --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d89b0c61..74e146a0 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/essentialkaos/check v1.4.0 - github.com/essentialkaos/depsy v1.2.0 + github.com/essentialkaos/depsy v1.3.0 github.com/essentialkaos/go-linenoise/v3 v3.6.0 golang.org/x/crypto v0.22.0 golang.org/x/sys v0.19.0 diff --git a/go.sum b/go.sum index 01561566..c8d358e6 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/essentialkaos/check v1.4.0 h1:kWdFxu9odCxUqo1NNFNJmguGrDHgwi3A8daXX1nkuKk= github.com/essentialkaos/check v1.4.0/go.mod h1:LMKPZ2H+9PXe7Y2gEoKyVAwUqXVgx7KtgibfsHJPus0= -github.com/essentialkaos/depsy v1.2.0 h1:vPuzyS/To7OEurdTDns23SMW0/A1Dw74wuV14YjmH6k= -github.com/essentialkaos/depsy v1.2.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8= +github.com/essentialkaos/depsy v1.3.0 h1:CN7bRgBU2jGTHSkg/Sh38eDUn7cvmaTp2sxFt2HpFeU= +github.com/essentialkaos/depsy v1.3.0/go.mod h1:kpiTAV17dyByVnrbNaMcZt2jRwvuXClUYOzpyJQwtG8= github.com/essentialkaos/go-linenoise/v3 v3.6.0 h1:deLcrodtLIkcHjNyW/MoQpjznXPVqvwlspxk7s/5YeY= github.com/essentialkaos/go-linenoise/v3 v3.6.0/go.mod h1:Fi6kLdZdURkXHpRkIiX2nFGORNv81CXTZ2Mn72i/cn0= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= From 03efd5a5cba7ac056cda8b9e5fa35164052d6525 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 2 May 2024 00:01:51 +0300 Subject: [PATCH 08/11] [support/deps] Add support for local path replacements --- support/support_nix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/support/support_nix.go b/support/support_nix.go index 8e974d5d..b06ce8d0 100644 --- a/support/support_nix.go +++ b/support/support_nix.go @@ -470,7 +470,7 @@ func (i *Info) printDependencies() { case "": fmtc.Printf(" {s}%8s{!} %s\n", dep.Version, dep.Path) default: - fmtc.Printf(" {s}%8s{!} %s {s-}(%s){!}\n", dep.Version, dep.Path, dep.Extra) + fmtc.Printf(" {s}%8s{!} %s {s-}(%s){!}\n", dep.Version, dep.Path, extra) } } } From 83623640986b6102f10e252dede402c60c71abb2 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 2 May 2024 00:04:36 +0300 Subject: [PATCH 09/11] [support/deps] Revert special output for local paths --- CHANGELOG.md | 1 - support/support_nix.go | 8 +------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93d6015a..b6e353f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,6 @@ ### 12.121.0 - `[initsystem/sdnotify]` Added new package for sending messages to systemd -- `[support/deps]` Added support for local path replacements ### 12.120.0 diff --git a/support/support_nix.go b/support/support_nix.go index b06ce8d0..23fa2678 100644 --- a/support/support_nix.go +++ b/support/support_nix.go @@ -460,17 +460,11 @@ func (i *Info) printDependencies() { fmtutil.Separator(false, "DEPENDENCIES") for _, dep := range i.Deps { - extra := dep.Extra - - if strutil.HasPrefixAny(extra, ".", "/") { - extra = "local-path: " + extra - } - switch dep.Extra { case "": fmtc.Printf(" {s}%8s{!} %s\n", dep.Version, dep.Path) default: - fmtc.Printf(" {s}%8s{!} %s {s-}(%s){!}\n", dep.Version, dep.Path, extra) + fmtc.Printf(" {s}%8s{!} %s {s-}(%s){!}\n", dep.Version, dep.Path, dep.Extra) } } } From 84a11a0673137db569cd0718584a3dfb34017a27 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 2 May 2024 01:32:49 +0300 Subject: [PATCH 10/11] [support/deps] Update compatibility with the latest version of depsy --- CHANGELOG.md | 1 + support/deps/deps.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6e353f4..afc0bd77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 12.121.0 - `[initsystem/sdnotify]` Added new package for sending messages to systemd +- `[support/deps]` Updated for compatibility with the latest version of [depsy](https://kaos.sh/depsy) ### 12.120.0 diff --git a/support/deps/deps.go b/support/deps/deps.go index 618c6f1e..d2277845 100644 --- a/support/deps/deps.go +++ b/support/deps/deps.go @@ -23,7 +23,7 @@ func Extract(gomod []byte) []support.Dep { for _, dep := range depsy.Extract(gomod, false) { result = append(result, support.Dep{ Version: dep.Version, - Path: dep.Path, + Path: dep.PrettyPath(), Extra: dep.Extra, }) } From 943922c1ef04ad6408d4b41bb5c6bf298fdc097a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Thu, 2 May 2024 19:18:21 +0300 Subject: [PATCH 11/11] [terminal/tty] Improve check for systemd --- CHANGELOG.md | 1 + terminal/tty/tty_posix.go | 3 +-- terminal/tty/tty_test.go | 7 +------ 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index afc0bd77..86006ced 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - `[initsystem/sdnotify]` Added new package for sending messages to systemd - `[support/deps]` Updated for compatibility with the latest version of [depsy](https://kaos.sh/depsy) +- `[terminal/tty]` Improved check for systemd ### 12.120.0 diff --git a/terminal/tty/tty_posix.go b/terminal/tty/tty_posix.go index f3adb6c7..f25174b8 100644 --- a/terminal/tty/tty_posix.go +++ b/terminal/tty/tty_posix.go @@ -46,8 +46,7 @@ func IsFakeTTY() bool { // IsSystemd returns true if process started by systemd func IsSystemd() bool { - return os.Getenv("INVOCATION_ID") != "" || - os.Getenv("SYSTEMCTL_IGNORE_DEPENDENCIES") != "" + return os.Getppid() == 1 } // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/terminal/tty/tty_test.go b/terminal/tty/tty_test.go index 28e22c94..27e3c975 100644 --- a/terminal/tty/tty_test.go +++ b/terminal/tty/tty_test.go @@ -48,12 +48,7 @@ func (s *TTYSuite) TestIsTTY(c *C) { } func (s *TTYSuite) TestIsSystemd(c *C) { - os.Setenv("INVOCATION_ID", "") - os.Setenv("SYSTEMCTL_IGNORE_DEPENDENCIES", "") - c.Assert(IsSystemd(), Equals, false) - os.Setenv("INVOCATION_ID", "5d0149bfa2c34b79bccb13074001eb20") - c.Assert(IsSystemd(), Equals, true) - os.Setenv("INVOCATION_ID", "") + IsSystemd() } func (s *TTYSuite) TestIsTMUX(c *C) {