Преглед на файлове

FreeBSD changes (#718) (#721)

* FreeBSD changes (#718)

* Update build Makefile to be able to build on other platforms

Split Makefiles to isolate every specifics per platform if specific file
is not found, the include will default to linux.

* Isolate linux specific messages emitted by crowdsec command line

On other platforms, we shouldn't write messages with `systemctl`
occurrences. This commit isolate the specific messages and ensure the
messages are relevant for the given platform according result of `GOOS`.

* remove the test

Co-authored-by: Sofian Brabez <sbz@6dev.net>
Co-authored-by: AlteredCoder <AlteredCoder>
AlteredCoder преди 4 години
родител
ревизия
20ccb32124

+ 9 - 2
Makefile

@@ -1,3 +1,12 @@
+ROOT= $(shell git rev-parse --show-toplevel)
+SYSTEM = $(shell uname -s | tr '[A-Z]' '[a-z]')
+
+ifneq ("$(wildcard $(ROOT)/platform/$(SYSTEM).mk)", "")
+	include $(ROOT)/platform/$(SYSTEM).mk
+else
+	include $(ROOT)/platform/linux.mk
+endif
+
 PREFIX?="/tmp/crowdsec/"
 CFG_PREFIX = $(PREFIX)"/etc/crowdsec/"
 BIN_PREFIX = $(PREFIX)"/usr/local/bin/"
@@ -9,10 +18,8 @@ CSCLI_FOLDER = "./cmd/crowdsec-cli/"
 CROWDSEC_BIN = "crowdsec"
 CSCLI_BIN = "cscli"
 BUILD_CMD = "build"
-MAKE = "make"
 
 GOARCH=amd64
-GOOS=linux
 
 #Golang version info
 GO_MAJOR_VERSION = $(shell go version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f1)

+ 1 - 1
cmd/crowdsec-cli/capi.go

@@ -99,7 +99,7 @@ func NewCapiCmd() *cobra.Command {
 				fmt.Printf("%s\n", string(apiConfigDump))
 			}
 
-			log.Warningf("Run 'sudo systemctl reload crowdsec' for the new configuration to be effective")
+			log.Warningf(ReloadMessage())
 		},
 	}
 	cmdCapiRegister.Flags().StringVarP(&outputFile, "file", "f", "", "output file destination")

+ 1 - 1
cmd/crowdsec-cli/collections.go

@@ -40,7 +40,7 @@ func NewCollectionsCmd() *cobra.Command {
 			if cmd.Name() == "inspect" || cmd.Name() == "list" {
 				return
 			}
-			log.Infof("Run 'sudo systemctl reload crowdsec' for the new configuration to be effective.")
+			log.Infof(ReloadMessage())
 		},
 	}
 

+ 1 - 1
cmd/crowdsec-cli/lapi.go

@@ -115,7 +115,7 @@ Keep in mind the machine needs to be validated by an administrator on LAPI side
 			} else {
 				fmt.Printf("%s\n", string(apiConfigDump))
 			}
-			log.Warningf("Run 'sudo systemctl reload crowdsec' for the new configuration to be effective")
+			log.Warningf(ReloadMessage())
 		},
 	}
 	cmdLapiRegister.Flags().StringVarP(&apiURL, "url", "u", "", "URL of the API (ie. http://127.0.0.1)")

+ 25 - 0
cmd/crowdsec-cli/messages.go

@@ -0,0 +1,25 @@
+package main
+
+import (
+	"fmt"
+	"runtime"
+)
+
+const (
+	ReloadMessageFormat = `Run '%s' for the new configuration to be effective.`
+	ReloadCmdLinux      = `sudo systemctl reload crowdsec`
+	ReloadCmdFreebsd    = `sudo service crowdsec reload`
+)
+
+func ReloadMessage() string {
+
+	var reloadCmd string
+
+	if runtime.GOOS == "freebsd" {
+		reloadCmd = ReloadCmdFreebsd
+	} else {
+		reloadCmd = ReloadCmdLinux
+	}
+
+	return fmt.Sprintf(ReloadMessageFormat, reloadCmd)
+}

+ 1 - 1
cmd/crowdsec-cli/parsers.go

@@ -43,7 +43,7 @@ cscli parsers remove crowdsecurity/sshd-logs
 			if cmd.Name() == "inspect" || cmd.Name() == "list" {
 				return
 			}
-			log.Infof("Run 'sudo systemctl reload crowdsec' for the new configuration to be effective.")
+			log.Infof(ReloadMessage())
 		},
 	}
 

+ 1 - 1
cmd/crowdsec-cli/postoverflows.go

@@ -42,7 +42,7 @@ func NewPostOverflowsCmd() *cobra.Command {
 			if cmd.Name() == "inspect" || cmd.Name() == "list" {
 				return
 			}
-			log.Infof("Run 'sudo systemctl reload crowdsec' for the new configuration to be effective.")
+			log.Infof(ReloadMessage())
 		},
 	}
 

+ 1 - 1
cmd/crowdsec-cli/scenarios.go

@@ -43,7 +43,7 @@ cscli scenarios remove crowdsecurity/ssh-bf
 			if cmd.Name() == "inspect" || cmd.Name() == "list" {
 				return
 			}
-			log.Infof("Run 'sudo systemctl reload crowdsec' for the new configuration to be effective.")
+			log.Infof(ReloadMessage())
 		},
 	}
 

+ 1 - 1
cmd/crowdsec-cli/simulation.go

@@ -118,7 +118,7 @@ cscli simulation disable crowdsecurity/ssh-bf`,
 		},
 		PersistentPostRun: func(cmd *cobra.Command, args []string) {
 			if cmd.Name() != "status" {
-				log.Infof("Run 'sudo systemctl reload crowdsec' for the new configuration to be effective.")
+				log.Infof(ReloadMessage())
 			}
 		},
 	}

+ 7 - 0
platform/freebsd.mk

@@ -0,0 +1,7 @@
+# FreeBSD specific
+#
+
+Make=gmake
+GOOS=freebsd
+
+$(warning Building for freebsd)

+ 6 - 0
platform/linux.mk

@@ -0,0 +1,6 @@
+# Linux specific
+
+MAKE=make
+GOOS=linux
+
+$(warning Building for linux)