From c8c2e7fdcd92c5db9ae84d92bd9da2c8397abe5e Mon Sep 17 00:00:00 2001
From: Levente Pap <levente.pap@iota.org>
Date: Thu, 7 May 2020 18:26:42 +0200
Subject: [PATCH] Terminate GoShimmer without config file (#395)

* Fix #355 Terminate GoShimmer without config file

 - `skip-config` flag to bypass check and supply values
   from cli

* Remove empty line

* Add some comments

* change termination comment

* lower case beginning of comments

* Don't mount config file into docker container by default

* Integration tests skip config file check

* Add flags for logger config

* Log at debug level for integration tests

* Update hive.go to latest version

* upgrade hive.go

Signed-off-by: Wolfgang Welz <welzwo@gmail.com>

Co-authored-by: Luca Moser <moser.luca@gmail.com>
Co-authored-by: jonastheis <mail@jonastheis.de>
Co-authored-by: Wolfgang Welz <welzwo@gmail.com>
---
 Dockerfile                                    |  2 --
 go.mod                                        |  2 +-
 go.sum                                        |  4 +--
 plugins/config/plugin.go                      | 30 +++++++++----------
 plugins/logger/parameters.go                  | 29 ++++++++++++++++++
 plugins/logger/plugin.go                      |  2 ++
 plugins/remotelog/plugin.go                   | 13 +++++---
 .../tester/framework/docker.go                |  4 +++
 tools/integration-tests/tester/go.mod         |  2 +-
 tools/integration-tests/tester/go.sum         |  4 +--
 10 files changed, 65 insertions(+), 27 deletions(-)
 create mode 100644 plugins/logger/parameters.go

diff --git a/Dockerfile b/Dockerfile
index eb159806..0df4a294 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -39,7 +39,5 @@ EXPOSE 14626/udp
 
 # Copy the Pre-built binary file from the previous stage
 COPY --from=build /go/bin/goshimmer /run/goshimmer
-# Copy the default config
-COPY config.default.json /config.json
 
 ENTRYPOINT ["/run/goshimmer", "--config-dir=/", "--database.directory=/tmp/mainnetdb"]
diff --git a/go.mod b/go.mod
index b10574b1..20518345 100644
--- a/go.mod
+++ b/go.mod
@@ -11,7 +11,7 @@ require (
 	github.com/gobuffalo/packr/v2 v2.7.1
 	github.com/golang/protobuf v1.3.5
 	github.com/gorilla/websocket v1.4.1
-	github.com/iotaledger/hive.go v0.0.0-20200430073924-0e16f8c3a522
+	github.com/iotaledger/hive.go v0.0.0-20200504205557-790e3a7585f2
 	github.com/iotaledger/iota.go v1.0.0-beta.14
 	github.com/labstack/echo v3.3.10+incompatible
 	github.com/labstack/gommon v0.3.0
diff --git a/go.sum b/go.sum
index d42e398e..fc13947f 100644
--- a/go.sum
+++ b/go.sum
@@ -145,8 +145,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
 github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
-github.com/iotaledger/hive.go v0.0.0-20200430073924-0e16f8c3a522 h1:pd7D0jtsF24UKgNTPxXxuZKzPbFKKoau4r066p6GtAo=
-github.com/iotaledger/hive.go v0.0.0-20200430073924-0e16f8c3a522/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
+github.com/iotaledger/hive.go v0.0.0-20200504205557-790e3a7585f2 h1:vDcNUncyinum1ly5FJPJfYNPZczeo5OLuEpoDu+mxks=
+github.com/iotaledger/hive.go v0.0.0-20200504205557-790e3a7585f2/go.mod h1:HgYsLMzyQV+eaiUrxa1c7qvH9Jwi2ncycqtlw+Lczhs=
 github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8=
 github.com/iotaledger/iota.go v1.0.0-beta.14 h1:Oeb28MfBuJEeXcGrLhTCJFtbsnc8y1u7xidsAmiOD5A=
 github.com/iotaledger/iota.go v1.0.0-beta.14/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8=
diff --git a/plugins/config/plugin.go b/plugins/config/plugin.go
index 596ad2b5..ab43e605 100644
--- a/plugins/config/plugin.go
+++ b/plugins/config/plugin.go
@@ -1,8 +1,10 @@
 package config
 
 import (
+	"fmt"
+	"os"
+
 	"github.com/iotaledger/hive.go/events"
-	"github.com/iotaledger/hive.go/logger"
 	"github.com/iotaledger/hive.go/node"
 	"github.com/iotaledger/hive.go/parameter"
 	flag "github.com/spf13/pflag"
@@ -17,21 +19,12 @@ var (
 	Plugin = node.NewPlugin(PluginName, node.Enabled)
 
 	// flags
-	configName    = flag.StringP("config", "c", "config", "Filename of the config file without the file extension")
-	configDirPath = flag.StringP("config-dir", "d", ".", "Path to the directory containing the config file")
+	configName          = flag.StringP("config", "c", "config", "Filename of the config file without the file extension")
+	configDirPath       = flag.StringP("config-dir", "d", ".", "Path to the directory containing the config file")
+	skipConfigAvailable = flag.Bool("skip-config", false, "Skip config file availability check")
 
 	// Node is viper
 	Node *viper.Viper
-
-	// logger
-	defaultLoggerConfig = logger.Config{
-		Level:             "info",
-		DisableCaller:     false,
-		DisableStacktrace: false,
-		Encoding:          "console",
-		OutputPaths:       []string{"goshimmer.log"},
-		DisableEvents:     false,
-	}
 )
 
 // Init triggers the Init event.
@@ -42,10 +35,17 @@ func Init() {
 func init() {
 	// set the default logger config
 	Node = viper.New()
-	Node.SetDefault(logger.ViperKey, defaultLoggerConfig)
 
 	Plugin.Events.Init.Attach(events.NewClosure(func(*node.Plugin) {
 		if err := fetch(false); err != nil {
+			if !*skipConfigAvailable {
+				// we wanted a config file but it was not present
+				// global logger instance is not initialized at this stage...
+				fmt.Println(err.Error())
+				fmt.Println("no config file present, terminating GoShimmer. please use the provided config.default.json to create a config.json.")
+				// daemon is not running yet, so we just exit
+				os.Exit(1)
+			}
 			panic(err)
 		}
 	}))
@@ -57,7 +57,7 @@ func init() {
 // and ending with: .json, .toml, .yaml or .yml (in this sequence).
 func fetch(printConfig bool, ignoreSettingsAtPrint ...[]string) error {
 	flag.Parse()
-	err := parameter.LoadConfigFile(Node, *configDirPath, *configName, true, true)
+	err := parameter.LoadConfigFile(Node, *configDirPath, *configName, true, *skipConfigAvailable)
 	if err != nil {
 		return err
 	}
diff --git a/plugins/logger/parameters.go b/plugins/logger/parameters.go
new file mode 100644
index 00000000..d5326d8e
--- /dev/null
+++ b/plugins/logger/parameters.go
@@ -0,0 +1,29 @@
+package logger
+
+import (
+	flag "github.com/spf13/pflag"
+)
+
+const (
+	// CfgLoggerLevel defines the logger's level.
+	CfgLoggerLevel = "logger.level"
+	// CfgLoggerDisableCaller defines whether to disable caller info.
+	CfgLoggerDisableCaller = "logger.disableCaller"
+	// CfgLoggerDisableStacktrace defines whether to disable stack trace info.
+	CfgLoggerDisableStacktrace = "logger.disableStacktrace"
+	// CfgLoggerEncoding defines the logger's encoding.
+	CfgLoggerEncoding = "logger.encoding"
+	// CfgLoggerOutputPaths defines the logger's output paths.
+	CfgLoggerOutputPaths = "logger.outputPaths"
+	// CfgLoggerDisableEvents defines whether to disable logger events.
+	CfgLoggerDisableEvents = "logger.disableEvents"
+)
+
+func initFlags() {
+	flag.String(CfgLoggerLevel, "info", "log level")
+	flag.Bool(CfgLoggerDisableCaller, false, "disable caller info in log")
+	flag.Bool(CfgLoggerDisableStacktrace, false, "disable stack trace in log")
+	flag.String(CfgLoggerEncoding, "console", "log encoding")
+	flag.StringSlice(CfgLoggerOutputPaths, []string{"stdout", "goshimmer.log"}, "log output paths")
+	flag.Bool(CfgLoggerDisableEvents, true, "disable logger events")
+}
diff --git a/plugins/logger/plugin.go b/plugins/logger/plugin.go
index 5ccfc62b..ebe14309 100644
--- a/plugins/logger/plugin.go
+++ b/plugins/logger/plugin.go
@@ -19,6 +19,8 @@ func Init() {
 }
 
 func init() {
+	initFlags()
+
 	Plugin.Events.Init.Attach(events.NewClosure(func(*node.Plugin) {
 		if err := logger.InitGlobalLogger(config.Node); err != nil {
 			panic(err)
diff --git a/plugins/remotelog/plugin.go b/plugins/remotelog/plugin.go
index 32f6ad6b..cd895c08 100644
--- a/plugins/remotelog/plugin.go
+++ b/plugins/remotelog/plugin.go
@@ -23,6 +23,7 @@ import (
 	"github.com/iotaledger/hive.go/logger"
 	"github.com/iotaledger/hive.go/node"
 	"github.com/iotaledger/hive.go/workerpool"
+	flag "github.com/spf13/pflag"
 	"gopkg.in/src-d/go-git.v4"
 )
 
@@ -38,8 +39,8 @@ type logMessage struct {
 }
 
 const (
-	// CfgServerAddress defines the config flag of the server address.
-	CfgServerAddress = "logger.remotelog.serverAddress"
+	// CfgLoggerRemotelogServerAddress defines the config flag of the server address.
+	CfgLoggerRemotelogServerAddress = "logger.remotelog.serverAddress"
 	// CfgDisableEvents defines the config flag for disabling logger events.
 	CfgDisableEvents = "logger.disableEvents"
 	// PluginName is the name of the remote log plugin.
@@ -57,6 +58,10 @@ var (
 	workerPool  *workerpool.WorkerPool
 )
 
+func init() {
+	flag.String(CfgLoggerRemotelogServerAddress, "remotelog.goshimmer.iota.cafe:5213", "RemoteLog server address")
+}
+
 func configure(plugin *node.Plugin) {
 	log = logger.NewLogger(PluginName)
 
@@ -65,9 +70,9 @@ func configure(plugin *node.Plugin) {
 		return
 	}
 
-	c, err := net.Dial("udp", config.Node.GetString(CfgServerAddress))
+	c, err := net.Dial("udp", config.Node.GetString(CfgLoggerRemotelogServerAddress))
 	if err != nil {
-		log.Fatalf("Could not create UDP socket to '%s'. %v", config.Node.GetString(CfgServerAddress), err)
+		log.Fatalf("Could not create UDP socket to '%s'. %v", config.Node.GetString(CfgLoggerRemotelogServerAddress), err)
 		return
 	}
 	conn = c
diff --git a/tools/integration-tests/tester/framework/docker.go b/tools/integration-tests/tester/framework/docker.go
index 19b37ce2..b00dc399 100644
--- a/tools/integration-tests/tester/framework/docker.go
+++ b/tools/integration-tests/tester/framework/docker.go
@@ -59,6 +59,8 @@ func (d *DockerContainer) CreateGoShimmerEntryNode(name string, seed string) err
 		Image:        "iotaledger/goshimmer",
 		ExposedPorts: nil,
 		Cmd: strslice.StrSlice{
+			"--skip-config=true",
+			"--logger.level=debug",
 			fmt.Sprintf("--node.disablePlugins=%s", disabledPluginsEntryNode),
 			"--autopeering.entryNodes=",
 			fmt.Sprintf("--autopeering.seed=%s", seed),
@@ -77,6 +79,8 @@ func (d *DockerContainer) CreateGoShimmerPeer(name string, seed string, entryNod
 			nat.Port("8080/tcp"): {},
 		},
 		Cmd: strslice.StrSlice{
+			"--skip-config=true",
+			"--logger.level=debug",
 			fmt.Sprintf("--node.disablePlugins=%s", disabledPluginsPeer),
 			fmt.Sprintf("--node.enablePlugins=%s", func() string {
 				if bootstrap {
diff --git a/tools/integration-tests/tester/go.mod b/tools/integration-tests/tester/go.mod
index 88740741..188087ae 100644
--- a/tools/integration-tests/tester/go.mod
+++ b/tools/integration-tests/tester/go.mod
@@ -9,7 +9,7 @@ require (
 	github.com/docker/go-connections v0.4.0
 	github.com/docker/go-units v0.4.0 // indirect
 	github.com/iotaledger/goshimmer v0.1.3
-	github.com/iotaledger/hive.go v0.0.0-20200430073924-0e16f8c3a522
+	github.com/iotaledger/hive.go v0.0.0-20200504205557-790e3a7585f2
 	github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
 	github.com/stretchr/testify v1.5.1
 )
diff --git a/tools/integration-tests/tester/go.sum b/tools/integration-tests/tester/go.sum
index 695293a3..132fe740 100644
--- a/tools/integration-tests/tester/go.sum
+++ b/tools/integration-tests/tester/go.sum
@@ -139,8 +139,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
-github.com/iotaledger/hive.go v0.0.0-20200430073924-0e16f8c3a522 h1:pd7D0jtsF24UKgNTPxXxuZKzPbFKKoau4r066p6GtAo=
-github.com/iotaledger/hive.go v0.0.0-20200430073924-0e16f8c3a522/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
+github.com/iotaledger/hive.go v0.0.0-20200504205557-790e3a7585f2 h1:vDcNUncyinum1ly5FJPJfYNPZczeo5OLuEpoDu+mxks=
+github.com/iotaledger/hive.go v0.0.0-20200504205557-790e3a7585f2/go.mod h1:HgYsLMzyQV+eaiUrxa1c7qvH9Jwi2ncycqtlw+Lczhs=
 github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8=
 github.com/iotaledger/iota.go v1.0.0-beta.14/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8=
 github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
-- 
GitLab