From bfed861ba7c99e9ea4be4a9e7e1d9748c180a224 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 31 Jan 2025 11:15:28 +0100 Subject: [PATCH] don't ask user to reload systemd service when running in docker (#3434) * don't ask user to reload systemd service when running in docker * refactor + give appropriate message if terminal is attached * remove explicit filetype --- Dockerfile | 1 - Dockerfile.debian | 1 - cmd/crowdsec-cli/clicapi/capi.go | 4 +++- cmd/crowdsec-cli/cliconsole/console.go | 8 +++++-- cmd/crowdsec-cli/clihub/hub.go | 4 ++-- cmd/crowdsec-cli/cliitem/cmdinstall.go | 4 ++-- cmd/crowdsec-cli/cliitem/cmdremove.go | 4 ++-- cmd/crowdsec-cli/cliitem/cmdupgrade.go | 4 ++-- cmd/crowdsec-cli/clilapi/register.go | 4 +++- cmd/crowdsec-cli/clisimulation/simulation.go | 4 ++-- cmd/crowdsec-cli/reload/message.go | 6 +++++ .../{reload_freebsd.go => message_freebsd.go} | 2 +- .../{reload_linux.go => message_linux.go} | 2 +- cmd/crowdsec-cli/reload/message_windows.go | 3 +++ cmd/crowdsec-cli/reload/reload.go | 22 +++++++++++++++---- cmd/crowdsec-cli/reload/reload_windows.go | 3 --- 16 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 cmd/crowdsec-cli/reload/message.go rename cmd/crowdsec-cli/reload/{reload_freebsd.go => message_freebsd.go} (64%) rename cmd/crowdsec-cli/reload/{reload_linux.go => message_linux.go} (62%) create mode 100644 cmd/crowdsec-cli/reload/message_windows.go delete mode 100644 cmd/crowdsec-cli/reload/reload_windows.go diff --git a/Dockerfile b/Dockerfile index ee6d54abb..d368f0f6e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,3 @@ -# vim: set ft=dockerfile: FROM docker.io/golang:1.23-alpine3.20 AS build ARG BUILD_VERSION diff --git a/Dockerfile.debian b/Dockerfile.debian index f37ba02a7..a9b58c633 100644 --- a/Dockerfile.debian +++ b/Dockerfile.debian @@ -1,4 +1,3 @@ -# vim: set ft=dockerfile: FROM docker.io/golang:1.23-bookworm AS build ARG BUILD_VERSION diff --git a/cmd/crowdsec-cli/clicapi/capi.go b/cmd/crowdsec-cli/clicapi/capi.go index 120acca8b..14637a26e 100644 --- a/cmd/crowdsec-cli/clicapi/capi.go +++ b/cmd/crowdsec-cli/clicapi/capi.go @@ -123,7 +123,9 @@ func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputF fmt.Println(string(apiConfigDump)) } - log.Warning(reload.Message) + if msg := reload.UserMessage(); msg != "" { + log.Warning(msg) + } return nil } diff --git a/cmd/crowdsec-cli/cliconsole/console.go b/cmd/crowdsec-cli/cliconsole/console.go index dbbe2487c..fcc128bd5 100644 --- a/cmd/crowdsec-cli/cliconsole/console.go +++ b/cmd/crowdsec-cli/cliconsole/console.go @@ -214,7 +214,9 @@ Enable given information push to the central API. Allows to empower the console` log.Infof("%v have been enabled", args) } - log.Info(reload.Message) + if reload.UserMessage() != "" { + log.Info(reload.UserMessage()) + } return nil }, @@ -248,7 +250,9 @@ Disable given information push to the central API.`, log.Infof("%v have been disabled", args) } - log.Info(reload.Message) + if msg := reload.UserMessage(); msg != "" { + log.Info(msg) + } return nil }, diff --git a/cmd/crowdsec-cli/clihub/hub.go b/cmd/crowdsec-cli/clihub/hub.go index 0d1e625f7..66fbe7c40 100644 --- a/cmd/crowdsec-cli/clihub/hub.go +++ b/cmd/crowdsec-cli/clihub/hub.go @@ -193,8 +193,8 @@ func (cli *cliHub) upgrade(ctx context.Context, yes bool, dryRun bool, force boo return err } - if plan.ReloadNeeded { - fmt.Println("\n" + reload.Message) + if msg := reload.UserMessage(); msg != "" && plan.ReloadNeeded { + fmt.Println("\n" + msg) } return nil diff --git a/cmd/crowdsec-cli/cliitem/cmdinstall.go b/cmd/crowdsec-cli/cliitem/cmdinstall.go index daddbe84a..74ffbe727 100644 --- a/cmd/crowdsec-cli/cliitem/cmdinstall.go +++ b/cmd/crowdsec-cli/cliitem/cmdinstall.go @@ -88,8 +88,8 @@ func (cli cliItem) install(ctx context.Context, args []string, yes bool, dryRun log.Error(err) } - if plan.ReloadNeeded { - fmt.Println("\n" + reload.Message) + if msg := reload.UserMessage(); msg != "" && plan.ReloadNeeded { + fmt.Println("\n" + msg) } return nil diff --git a/cmd/crowdsec-cli/cliitem/cmdremove.go b/cmd/crowdsec-cli/cliitem/cmdremove.go index ac9410c04..c8ea041ac 100644 --- a/cmd/crowdsec-cli/cliitem/cmdremove.go +++ b/cmd/crowdsec-cli/cliitem/cmdremove.go @@ -104,8 +104,8 @@ func (cli cliItem) remove(ctx context.Context, args []string, yes bool, dryRun b return err } - if plan.ReloadNeeded { - fmt.Println("\n" + reload.Message) + if msg := reload.UserMessage(); msg != "" && plan.ReloadNeeded { + fmt.Println("\n" + msg) } return nil diff --git a/cmd/crowdsec-cli/cliitem/cmdupgrade.go b/cmd/crowdsec-cli/cliitem/cmdupgrade.go index 1ddd07485..5320bc04b 100644 --- a/cmd/crowdsec-cli/cliitem/cmdupgrade.go +++ b/cmd/crowdsec-cli/cliitem/cmdupgrade.go @@ -66,8 +66,8 @@ func (cli cliItem) upgrade(ctx context.Context, args []string, yes bool, dryRun return err } - if plan.ReloadNeeded { - fmt.Println("\n" + reload.Message) + if msg := reload.UserMessage(); msg != "" && plan.ReloadNeeded { + fmt.Println("\n" + msg) } return nil diff --git a/cmd/crowdsec-cli/clilapi/register.go b/cmd/crowdsec-cli/clilapi/register.go index e8eb7ddc5..7430c73c3 100644 --- a/cmd/crowdsec-cli/clilapi/register.go +++ b/cmd/crowdsec-cli/clilapi/register.go @@ -87,7 +87,9 @@ func (cli *cliLapi) register(ctx context.Context, apiURL string, outputFile stri fmt.Printf("%s\n", string(apiConfigDump)) } - log.Warning(reload.Message) + if msg := reload.UserMessage(); msg != "" { + log.Warning(msg) + } return nil } diff --git a/cmd/crowdsec-cli/clisimulation/simulation.go b/cmd/crowdsec-cli/clisimulation/simulation.go index c06db56f2..1b46c70c9 100644 --- a/cmd/crowdsec-cli/clisimulation/simulation.go +++ b/cmd/crowdsec-cli/clisimulation/simulation.go @@ -47,8 +47,8 @@ cscli simulation disable crowdsecurity/ssh-bf`, return nil }, PersistentPostRun: func(cmd *cobra.Command, _ []string) { - if cmd.Name() != "status" { - log.Info(reload.Message) + if msg := reload.UserMessage(); msg != "" && cmd.Name() != "status" { + log.Info(msg) } }, } diff --git a/cmd/crowdsec-cli/reload/message.go b/cmd/crowdsec-cli/reload/message.go new file mode 100644 index 000000000..cd8e7d479 --- /dev/null +++ b/cmd/crowdsec-cli/reload/message.go @@ -0,0 +1,6 @@ +//go:build !windows && !freebsd && !linux + +package reload + +// generic message since we don't know the platform +const message = "Please reload the crowdsec process for the new configuration to be effective." diff --git a/cmd/crowdsec-cli/reload/reload_freebsd.go b/cmd/crowdsec-cli/reload/message_freebsd.go similarity index 64% rename from cmd/crowdsec-cli/reload/reload_freebsd.go rename to cmd/crowdsec-cli/reload/message_freebsd.go index 0dac99f23..9328f935b 100644 --- a/cmd/crowdsec-cli/reload/reload_freebsd.go +++ b/cmd/crowdsec-cli/reload/message_freebsd.go @@ -1,4 +1,4 @@ package reload // actually sudo is not that popular on freebsd, but this will do -const Message = "Run 'sudo service crowdsec reload' for the new configuration to be effective." +const message = "Run 'sudo service crowdsec reload' for the new configuration to be effective." diff --git a/cmd/crowdsec-cli/reload/reload_linux.go b/cmd/crowdsec-cli/reload/message_linux.go similarity index 62% rename from cmd/crowdsec-cli/reload/reload_linux.go rename to cmd/crowdsec-cli/reload/message_linux.go index fbe16e5f1..11c951653 100644 --- a/cmd/crowdsec-cli/reload/reload_linux.go +++ b/cmd/crowdsec-cli/reload/message_linux.go @@ -1,4 +1,4 @@ package reload // assume systemd, although gentoo and others may differ -const Message = "Run 'sudo systemctl reload crowdsec' for the new configuration to be effective." +const message = "Run 'sudo systemctl reload crowdsec' for the new configuration to be effective." diff --git a/cmd/crowdsec-cli/reload/message_windows.go b/cmd/crowdsec-cli/reload/message_windows.go new file mode 100644 index 000000000..888cb44b0 --- /dev/null +++ b/cmd/crowdsec-cli/reload/message_windows.go @@ -0,0 +1,3 @@ +package reload + +const message = "Please restart the crowdsec service for the new configuration to be effective." diff --git a/cmd/crowdsec-cli/reload/reload.go b/cmd/crowdsec-cli/reload/reload.go index fe03af1ea..44d001fda 100644 --- a/cmd/crowdsec-cli/reload/reload.go +++ b/cmd/crowdsec-cli/reload/reload.go @@ -1,6 +1,20 @@ -//go:build !windows && !freebsd && !linux - package reload -// generic message since we don't know the platform -const Message = "Please reload the crowdsec process for the new configuration to be effective." +import ( + "os" + + "github.com/crowdsecurity/go-cs-lib/version" + isatty "github.com/mattn/go-isatty" +) + +func UserMessage() string { + if version.System == "docker" { + if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) { + return "You may need to restart the container to apply the changes." + } + + return "" + } + + return message +} diff --git a/cmd/crowdsec-cli/reload/reload_windows.go b/cmd/crowdsec-cli/reload/reload_windows.go deleted file mode 100644 index 88642425a..000000000 --- a/cmd/crowdsec-cli/reload/reload_windows.go +++ /dev/null @@ -1,3 +0,0 @@ -package reload - -const Message = "Please restart the crowdsec service for the new configuration to be effective."