workaround for the restart command may fail in container (since fail2ban runs on the host)

This commit is contained in:
2025-02-27 14:03:08 +01:00
parent 9a66d2c87a
commit 8c84909c95
3 changed files with 27 additions and 29 deletions

View File

@@ -439,13 +439,22 @@ func RestartFail2banHandler(c *gin.Context) {
// return
// }
// Then restart
if err := fail2ban.RestartFail2ban(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
// Attempt to restart the fail2ban service.
restartErr := fail2ban.RestartFail2ban()
if restartErr != nil {
// Check if running inside a container.
if _, container := os.LookupEnv("CONTAINER"); container {
// In a container, the restart command may fail (since fail2ban runs on the host).
// Log the error and continue, so we can mark the restart as done.
log.Printf("Warning: restart failed inside container (expected behavior): %v", restartErr)
} else {
// On the host, a restart error is not acceptable.
c.JSON(http.StatusInternalServerError, gin.H{"error": restartErr.Error()})
return
}
}
// We set restart done in config
// Only call MarkRestartDone if we either successfully restarted the service or we are in a container.
if err := config.MarkRestartDone(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return