Skip to content

Commit

Permalink
don't ask user to reload systemd service when running in docker (#3434)
Browse files Browse the repository at this point in the history
* don't ask user to reload systemd service when running in docker

* refactor + give appropriate message if terminal is attached

* remove explicit filetype
  • Loading branch information
mmetc authored Jan 31, 2025
1 parent 763959f commit bfed861
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 25 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# vim: set ft=dockerfile:
FROM docker.io/golang:1.23-alpine3.20 AS build

ARG BUILD_VERSION
Expand Down
1 change: 0 additions & 1 deletion Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# vim: set ft=dockerfile:
FROM docker.io/golang:1.23-bookworm AS build

ARG BUILD_VERSION
Expand Down
4 changes: 3 additions & 1 deletion cmd/crowdsec-cli/clicapi/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/crowdsec-cli/cliconsole/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clihub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/cliitem/cmdinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/cliitem/cmdremove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/cliitem/cmdupgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion cmd/crowdsec-cli/clilapi/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/clisimulation/simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/crowdsec-cli/reload/message.go
Original file line number Diff line number Diff line change
@@ -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."
Original file line number Diff line number Diff line change
@@ -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."
Original file line number Diff line number Diff line change
@@ -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."
3 changes: 3 additions & 0 deletions cmd/crowdsec-cli/reload/message_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package reload

const message = "Please restart the crowdsec service for the new configuration to be effective."
22 changes: 18 additions & 4 deletions cmd/crowdsec-cli/reload/reload.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 0 additions & 3 deletions cmd/crowdsec-cli/reload/reload_windows.go

This file was deleted.

0 comments on commit bfed861

Please sign in to comment.