Explorar o código

feat: export config path (#2059)

* export config path

Signed-off-by: yisaer <disxiaofei@163.com>

* add code

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* add test

Signed-off-by: yisaer <disxiaofei@163.com>

* fix test

Signed-off-by: yisaer <disxiaofei@163.com>

* fix test

Signed-off-by: yisaer <disxiaofei@163.com>

* add doc

Signed-off-by: yisaer <disxiaofei@163.com>

* add doc

Signed-off-by: yisaer <disxiaofei@163.com>

* add doc

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

* address the comment

Signed-off-by: yisaer <disxiaofei@163.com>

---------

Signed-off-by: yisaer <disxiaofei@163.com>
Song Gao hai 1 ano
pai
achega
a6ef156f83

+ 19 - 1
cmd/kuiper/main.go

@@ -17,6 +17,7 @@ package main
 import (
 	"bufio"
 	"encoding/json"
+	"flag"
 	"fmt"
 	"net/rpc"
 	"os"
@@ -57,8 +58,25 @@ var (
 	LoadFileType = "relative"
 )
 
+var (
+	loadFileType string
+	etcPath      string
+)
+
+func init() {
+	flag.StringVar(&loadFileType, "loadFileTye", "", "loadFileType indicates the how to load path")
+	flag.StringVar(&etcPath, "etc", "", "etc indicates the path of etc dir")
+	if len(loadFileType) > 0 {
+		conf.PathConfig.LoadFileType = loadFileType
+	} else {
+		conf.PathConfig.LoadFileType = LoadFileType
+	}
+	if len(etcPath) > 0 {
+		conf.PathConfig.Dirs["etc"] = etcPath
+	}
+}
+
 func main() {
-	conf.LoadFileType = LoadFileType
 	app := cli.NewApp()
 	app.Version = Version
 

+ 37 - 1
cmd/kuiperd/main.go

@@ -15,6 +15,9 @@
 package main
 
 import (
+	"flag"
+
+	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/lf-edge/ekuiper/internal/server"
 )
 
@@ -23,6 +26,39 @@ var (
 	LoadFileType = "relative"
 )
 
+var (
+	loadFileType string
+	etcPath      string
+	dataPath     string
+	logPath      string
+	pluginsPath  string
+)
+
+func init() {
+	flag.StringVar(&loadFileType, "loadFileTye", "", "loadFileType indicates the how to load path")
+	flag.StringVar(&etcPath, "etc", "", "etc indicates the path of etc dir")
+	flag.StringVar(&dataPath, "data", "", "data indicates the path of data dir")
+	flag.StringVar(&logPath, "log", "", "log indicates the path of log dir")
+	flag.StringVar(&pluginsPath, "plugins", "", "plugins indicates the path of plugins dir")
+	if len(loadFileType) > 0 {
+		conf.PathConfig.LoadFileType = loadFileType
+	} else {
+		conf.PathConfig.LoadFileType = LoadFileType
+	}
+	if len(etcPath) > 0 {
+		conf.PathConfig.Dirs["etc"] = etcPath
+	}
+	if len(dataPath) > 0 {
+		conf.PathConfig.Dirs["data"] = dataPath
+	}
+	if len(logPath) > 0 {
+		conf.PathConfig.Dirs["log"] = logPath
+	}
+	if len(pluginsPath) > 0 {
+		conf.PathConfig.Dirs["plugins"] = pluginsPath
+	}
+}
+
 func main() {
-	server.StartUp(Version, LoadFileType)
+	server.StartUp(Version)
 }

+ 18 - 0
docs/en_US/configuration/configuration.md

@@ -34,3 +34,21 @@ CONNECTION__EDGEX__REDISMSGBUS__PORT => edgex.redismsgbus.port int etc/connectio
 ```
 
 The environment variables are separated by "__", the content of the first part after the separation matches the file name of the configuration file, and the remaining content matches the different levels of the configuration items. The file name could be `KUIPER` and `MQTT_SOURCE` in the `etc` folder; or  `CONNECTION` in `etc/connection` folder. Otherwise, the file should in `etc/sources` folder.
+
+### command line parameters
+
+eKuiper supports importing configuration from command line parameters, as follows:
+
+| configuration name | type | configuration role |
+|-----------------|--------|---------------------- --------------------------------|
+| loadFileType | string | Set the way to load files, support "relative" and "absolute" |
+| etc | string | Set absolute path of etc directory, only valid when loadFileType is "absolute" |
+| data | string | Set the absolute path of the data directory, only valid when loadFileType is "absolute" |
+| log | string | Set the absolute path of the log directory, only valid when loadFileType is "absolute" |
+| plugins | string | Set the absolute path of the plugins directory, only valid when loadFileType is "absolute" |
+
+example:
+
+```sh
+./bin/kuiperd -loadFileType absolute -etc /etc/kuiper
+```

+ 18 - 0
docs/zh_CN/configuration/configuration.md

@@ -35,3 +35,21 @@ CONNECTION__EDGEX__REDISMSGBUS__PORT => edgex.redismsgbus.port int etc/connectio
 
 环境变量用`__`分隔,分隔后的第一部分内容与配置文件的文件名匹配,其余内容与不同级别的配置项匹配。文件名可以是 `etc` 文件夹中的 `KUIPER` 和 `MQTT_SOURCE`;或
  `etc/connection` 文件夹中的 `CONNECTION`。其余情况,映射文件应在 `etc/sources` 文件夹下。
+
+### 命令行参数
+
+eKuiper 支持从命令行参数的方式传入配置,如下:
+
+| 配置名           | 类型   |       配置作用                                        |
+|-----------------|--------|-----------------------------------------------------|
+| loadFileType     | string | 设置加载文件的方式,支持 "relative" 与 "absolute" 两种方式      |
+| etc             | string | 设置 etc 目录的绝对路径,只有当 loadFileType 是 "absolute" 时有效                                |
+| data            | string | 设置 data 目录的绝对路径,只有当 loadFileType 是 "absolute" 时有效                               |
+| log             | string | 设置 log 目录的绝对路径,只有当 loadFileType 是 "absolute" 时有效                                |
+| plugins         | string | 设置 plugins 目录的绝对路径,只有当 loadFileType 是 "absolute" 时有效                            |
+
+举例如下:
+
+```sh
+./bin/kuiperd -loadFileType absolute -etc /etc/kuiper
+```

+ 14 - 4
internal/conf/path.go

@@ -22,6 +22,16 @@ import (
 	"strings"
 )
 
+func init() {
+	PathConfig.LoadFileType = "relative"
+	PathConfig.Dirs = AbsoluteMapping
+}
+
+type PathConfigure struct {
+	LoadFileType string
+	Dirs         map[string]string
+}
+
 const (
 	etcDir          = "etc"
 	dataDir         = "data"
@@ -32,7 +42,7 @@ const (
 )
 
 var (
-	LoadFileType    = "relative"
+	PathConfig      PathConfigure
 	AbsoluteMapping = map[string]string{
 		etcDir:     "/etc/kuiper",
 		dataDir:    "/var/lib/kuiper/data",
@@ -68,7 +78,7 @@ func GetPluginsLoc() (string, error) {
 }
 
 func absolutePath(loc string) (dir string, err error) {
-	for relDir, absoluteDir := range AbsoluteMapping {
+	for relDir, absoluteDir := range PathConfig.Dirs {
 		if strings.HasPrefix(loc, relDir) {
 			dir = strings.Replace(loc, relDir, absoluteDir, 1)
 			break
@@ -82,11 +92,11 @@ func absolutePath(loc string) (dir string, err error) {
 
 // GetLoc subdir must be a relative path
 func GetLoc(subdir string) (string, error) {
-	if "relative" == LoadFileType {
+	if "relative" == PathConfig.LoadFileType {
 		return relativePath(subdir)
 	}
 
-	if "absolute" == LoadFileType {
+	if "absolute" == PathConfig.LoadFileType {
 		return absolutePath(subdir)
 	}
 	return "", fmt.Errorf("Unrecognized loading method.")

+ 41 - 0
internal/conf/path_test.go

@@ -17,6 +17,8 @@ package conf
 import (
 	"strings"
 	"testing"
+
+	"github.com/stretchr/testify/require"
 )
 
 func TestAbsolutePath(t *testing.T) {
@@ -58,3 +60,42 @@ func TestGetDataLoc_Funcs(t *testing.T) {
 		t.Errorf("Unexpected data location %s", d)
 	}
 }
+
+func TestPathConfig(t *testing.T) {
+	PathConfig.Dirs["etc"] = "/etc/kuiper"
+	PathConfig.Dirs["data"] = "/data/kuiper"
+	PathConfig.Dirs["log"] = "/log/kuiper"
+	PathConfig.Dirs["plugins"] = "/tmp/plugins"
+
+	testcases := []struct {
+		dir    string
+		expect string
+	}{
+		{
+			dir:    etcDir,
+			expect: "/etc/kuiper",
+		},
+		{
+			dir:    dataDir,
+			expect: "/data/kuiper",
+		},
+		{
+			dir:    logDir,
+			expect: "/log/kuiper",
+		},
+		{
+			dir:    pluginsDir,
+			expect: "/tmp/plugins",
+		},
+		{
+			dir:    "etc/source",
+			expect: "/etc/kuiper/source",
+		},
+	}
+
+	for _, tc := range testcases {
+		d, err := absolutePath(tc.dir)
+		require.NoError(t, err)
+		require.Equal(t, tc.expect, d)
+	}
+}

+ 1 - 2
internal/server/server.go

@@ -82,9 +82,8 @@ func createPaths() {
 	}
 }
 
-func StartUp(Version, LoadFileType string) {
+func StartUp(Version string) {
 	version = Version
-	conf.LoadFileType = LoadFileType
 	startTimeStamp = time.Now().Unix()
 	createPaths()
 	conf.InitConf()