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
This commit is contained in:
mmetc 2025-01-31 11:15:28 +01:00 committed by GitHub
parent 763959fb68
commit bfed861ba7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 51 additions and 25 deletions

View file

@ -1,4 +1,3 @@
# vim: set ft=dockerfile:
FROM docker.io/golang:1.23-alpine3.20 AS build
ARG BUILD_VERSION

View file

@ -1,4 +1,3 @@
# vim: set ft=dockerfile:
FROM docker.io/golang:1.23-bookworm AS build
ARG BUILD_VERSION

View file

@ -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
}

View file

@ -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
},

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
}

View file

@ -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)
}
},
}

View file

@ -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."

View file

@ -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."

View file

@ -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."

View file

@ -0,0 +1,3 @@
package reload
const message = "Please restart the crowdsec service for the new configuration to be effective."

View file

@ -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
}

View file

@ -1,3 +0,0 @@
package reload
const Message = "Please restart the crowdsec service for the new configuration to be effective."