From d71b1b2b7f9531f9afc9db706cd5bd4136c746a3 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Mon, 10 Jun 2024 13:19:56 +0900 Subject: [PATCH] bug fix: environment variables and configs --- .gitignore | 6 ++-- Dockerfile | 14 ++++----- cmd/cm-beetle/main.go | 6 ++-- conf/config.yaml | 22 +++++++------- conf/log_conf.yaml | 2 +- conf/setup.env | 22 +++++++------- conf/template-config.yaml | 22 +++++++------- conf/template-setup.env | 22 +++++++------- deployments/kubernetes/deployment.yaml | 42 ++++++++++++++++---------- pkg/config/README.md | 18 ++++++----- pkg/config/config.go | 6 ++-- pkg/core/common/config.go | 6 ++-- pkg/logger/logger.go | 6 ++-- 13 files changed, 103 insertions(+), 91 deletions(-) diff --git a/.gitignore b/.gitignore index eb6973e..84d6f96 100644 --- a/.gitignore +++ b/.gitignore @@ -39,9 +39,9 @@ pkg/testclient/scripts/sequentialFullTest/ansibleAutoConf/* pkg/testclient/scripts/credentials* # Swagger docs for REST APIs -pkg/api/rest/docs/docs.go -pkg/api/rest/docs/swagger.json -pkg/api/rest/docs/swagger.yaml +# api/docs.go +# api/swagger.json +# api/swagger.yaml # Config file for CM-Beetle !conf/ diff --git a/Dockerfile b/Dockerfile index b1242f3..8f13b6e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,20 +43,20 @@ COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/api/ /app/api/ ## Set environment variables # Set system endpoints -ENV CMBEETLE_ROOT=/app \ +ENV BEETLE_ROOT=/app \ CBSTORE_ROOT=/app \ CBLOG_ROOT=/app -# ENV CBSPIDER_CALLMETHOD REST \ -# CBSPIDER_REST_URL http://cb-spider:1024/spider +# ENV SPIDER_CALL_METHOD REST \ +# SPIDER_REST_URL http://cb-spider:1024/spider -ENV CBTUMBLEBUG_CALLMETHOD=REST \ - CBTUMBLEBUG_REST_URL=http://localhost:1323/tumblebug +ENV TUMBLEBUG_CALL_METHOD=REST \ + TUMBLEBUG_REST_URL=http://localhost:1323/tumblebug ## Logger configuration -# Set log file path (default logfile path: ./cm-beetle.log) +# Set log file path (default logfile path: ./beetle.log) # Set log level, such as trace, debug info, warn, error, fatal, and panic -ENV LOGFILE_PATH=$CMBEETLE_ROOT/log/cm-beetle.log \ +ENV LOGFILE_PATH=$BEETLE_ROOT/log/beetle.log \ LOGFILE_MAXSIZE=10 \ LOGFILE_MAXBACKUPS=3 \ LOGFILE_MAXAGE=30 \ diff --git a/cmd/cm-beetle/main.go b/cmd/cm-beetle/main.go index 8b65060..07243ee 100644 --- a/cmd/cm-beetle/main.go +++ b/cmd/cm-beetle/main.go @@ -57,9 +57,9 @@ func main() { // common.DragonflyRestUrl = common.NVL(os.Getenv("DRAGONFLY_REST_URL"), "http://localhost:9090/dragonfly") common.TumblebugRestUrl = common.NVL(os.Getenv("TUMBLEBUG_REST_URL"), "http://localhost:1323/tumblebug") common.DBUrl = common.NVL(os.Getenv("DB_URL"), "localhost:3306") - common.DBDatabase = common.NVL(os.Getenv("DB_DATABASE"), "cb_beetle") - common.DBUser = common.NVL(os.Getenv("DB_USER"), "cb_beetle") - common.DBPassword = common.NVL(os.Getenv("DB_PASSWORD"), "cb_beetle") + common.DBDatabase = common.NVL(os.Getenv("DB_DATABASE"), "cm_beetle") + common.DBUser = common.NVL(os.Getenv("DB_USER"), "cm_beetle") + common.DBPassword = common.NVL(os.Getenv("DB_PASSWORD"), "cm_beetle") common.AutocontrolDurationMs = common.NVL(os.Getenv("AUTOCONTROL_DURATION_MS"), "10000") // load the latest configuration from DB (if exist) diff --git a/conf/config.yaml b/conf/config.yaml index f229ba9..2e4c8f8 100644 --- a/conf/config.yaml +++ b/conf/config.yaml @@ -1,27 +1,27 @@ ## Set system endpoints -cmbeetle: +beetle: root: # To be set in runtime cbstore: - root: # To be set in runtime (based on cmbeetle.root) + root: # To be set in runtime (based on beetle.root) cblog: - root: # To be set in runtime (based on cmbeetle.root) + root: # To be set in runtime (based on beetle.root) -cbspider: - callmethod: REST +spider: + call_method: REST rest: url: http://localhost:1024/spider -cbtumblebug: - callmethod: REST +tumblebug: + call_method: REST rest: url: http://localhost:1323/tumblebug ## Logger configuration logfile: - # Set log file path (default logfile path: ./cm-beetle.log) - path: ./cm-beetle.log + # Set log file path (default logfile path: ./beetle.log) + path: ./beetle.log maxsize: 10 maxbackups: 3 maxage: 30 @@ -68,5 +68,5 @@ self: ## Environment variables that you don't need to touch # Swagger UI API document file path apidoc: - # export APIDOC_PATH=$CMBEETLE_ROOT/api/swagger.json - path: # To be set in runtime (based on cmbeetle.root) + # export APIDOC_PATH=$BEETLE_ROOT/api/swagger.json + path: # To be set in runtime (based on beetle.root) diff --git a/conf/log_conf.yaml b/conf/log_conf.yaml index aa4024e..bed04b1 100644 --- a/conf/log_conf.yaml +++ b/conf/log_conf.yaml @@ -12,7 +12,7 @@ cblog: ## Config for File Output ## logfileinfo: - filename: $CMBEETLE_ROOT/log/cblogs.log + filename: $BEETLE_ROOT/log/cblogs.log maxsize: 10 # megabytes maxbackups: 50 maxage: 31 # days diff --git a/conf/setup.env b/conf/setup.env index dee219c..1db437d 100644 --- a/conf/setup.env +++ b/conf/setup.env @@ -1,17 +1,17 @@ ## Set system endpoints -# Set CMBEETLE_ROOT based on path of setup.env relatively +# Set BEETLE_ROOT based on path of setup.env relatively SCRIPT_DIR=`dirname ${BASH_SOURCE[0]-$0}` -export CMBEETLE_ROOT=`cd $SCRIPT_DIR && cd .. && pwd` -export CBSTORE_ROOT=$CMBEETLE_ROOT -export CBLOG_ROOT=$CMBEETLE_ROOT -#export CBSPIDER_CALLMETHOD=REST -#export CBSPIDER_REST_URL=http://localhost:1024/spider -export CBTUMBLEBUG_CALLMETHOD=REST -export CBTUMBLEBUG_REST_URL=http://localhost:1323/tumblebug +export BEETLE_ROOT=`cd $SCRIPT_DIR && cd .. && pwd` +export CBSTORE_ROOT=$BEETLE_ROOT +export CBLOG_ROOT=$BEETLE_ROOT +#export SPIDER_CALL_METHOD=REST +#export SPIDER_REST_URL=http://localhost:1024/spider +export TUMBLEBUG_CALL_METHOD=REST +export TUMBLEBUG_REST_URL=http://localhost:1323/tumblebug ## Logger configuration -# Set log file path (default logfile path: ./cm-beetle.log) -export LOGFILE_PATH=cm-beetle.log +# Set log file path (default logfile path: ./beetle.log) +export LOGFILE_PATH=beetle.log export LOGFILE_MAXSIZE=10 export LOGFILE_MAXBACKUPS=3 export LOGFILE_MAXAGE=30 @@ -45,4 +45,4 @@ export SELF_ENDPOINT=localhost:8056 ## Environment variables that you don't need to touch # Swagger UI API document file path -export APIDOC_PATH=$CMBEETLE_ROOT/api/swagger.json \ No newline at end of file +export APIDOC_PATH=$BEETLE_ROOT/api/swagger.json \ No newline at end of file diff --git a/conf/template-config.yaml b/conf/template-config.yaml index f229ba9..2e4c8f8 100644 --- a/conf/template-config.yaml +++ b/conf/template-config.yaml @@ -1,27 +1,27 @@ ## Set system endpoints -cmbeetle: +beetle: root: # To be set in runtime cbstore: - root: # To be set in runtime (based on cmbeetle.root) + root: # To be set in runtime (based on beetle.root) cblog: - root: # To be set in runtime (based on cmbeetle.root) + root: # To be set in runtime (based on beetle.root) -cbspider: - callmethod: REST +spider: + call_method: REST rest: url: http://localhost:1024/spider -cbtumblebug: - callmethod: REST +tumblebug: + call_method: REST rest: url: http://localhost:1323/tumblebug ## Logger configuration logfile: - # Set log file path (default logfile path: ./cm-beetle.log) - path: ./cm-beetle.log + # Set log file path (default logfile path: ./beetle.log) + path: ./beetle.log maxsize: 10 maxbackups: 3 maxage: 30 @@ -68,5 +68,5 @@ self: ## Environment variables that you don't need to touch # Swagger UI API document file path apidoc: - # export APIDOC_PATH=$CMBEETLE_ROOT/api/swagger.json - path: # To be set in runtime (based on cmbeetle.root) + # export APIDOC_PATH=$BEETLE_ROOT/api/swagger.json + path: # To be set in runtime (based on beetle.root) diff --git a/conf/template-setup.env b/conf/template-setup.env index ce6e57e..1db437d 100644 --- a/conf/template-setup.env +++ b/conf/template-setup.env @@ -1,17 +1,17 @@ ## Set system endpoints -# Set CMBEETLE_ROOT based on path of setup.env relatively +# Set BEETLE_ROOT based on path of setup.env relatively SCRIPT_DIR=`dirname ${BASH_SOURCE[0]-$0}` -export CMBEETLE_ROOT=`cd $SCRIPT_DIR && cd .. && pwd` -export CBSTORE_ROOT=$CMBEETLE_ROOT -export CBLOG_ROOT=$CMBEETLE_ROOT -#export CBSPIDER_CALLMETHOD=REST -#export CBSPIDER_REST_URL=http://localhost:1024/spider -#export CBTUMBLEBUG_CALLMETHOD=REST -#export CBTUMBLEBUG_REST_URL=http://localhost:1323/tumblebug +export BEETLE_ROOT=`cd $SCRIPT_DIR && cd .. && pwd` +export CBSTORE_ROOT=$BEETLE_ROOT +export CBLOG_ROOT=$BEETLE_ROOT +#export SPIDER_CALL_METHOD=REST +#export SPIDER_REST_URL=http://localhost:1024/spider +export TUMBLEBUG_CALL_METHOD=REST +export TUMBLEBUG_REST_URL=http://localhost:1323/tumblebug ## Logger configuration -# Set log file path (default logfile path: ./cm-beetle.log) -export LOGFILE_PATH=cm-beetle.log +# Set log file path (default logfile path: ./beetle.log) +export LOGFILE_PATH=beetle.log export LOGFILE_MAXSIZE=10 export LOGFILE_MAXBACKUPS=3 export LOGFILE_MAXAGE=30 @@ -45,4 +45,4 @@ export SELF_ENDPOINT=localhost:8056 ## Environment variables that you don't need to touch # Swagger UI API document file path -export APIDOC_PATH=$CMBEETLE_ROOT/api/swagger.json \ No newline at end of file +export APIDOC_PATH=$BEETLE_ROOT/api/swagger.json \ No newline at end of file diff --git a/deployments/kubernetes/deployment.yaml b/deployments/kubernetes/deployment.yaml index 110703d..2cfa712 100644 --- a/deployments/kubernetes/deployment.yaml +++ b/deployments/kubernetes/deployment.yaml @@ -3,7 +3,7 @@ apiVersion: apps/v1 kind: Deployment metadata: name: cm-beetle-deployment - namespace: cloud-barista # Change this to your desired namespace + namespace: cloud-barista # Change this to your desired namespace spec: replicas: 1 selector: @@ -16,18 +16,28 @@ spec: app: cm-beetle spec: containers: - - name: cm-beetle - image: ghcr.io/cloud-barista/cm-beetle:latest - ports: - - containerPort: 8056 - env: - - name: API_USERNAME - value: "default" - - name: API_PASSWORD - value: "default" - - name: AUTOCONTROL_DURATION_MS - value: "10000" - - name: SELF_ENDPOINT - value: "localhost:1323" - - name: API_DOC_PATH - value: "/app/pkg/api/rest/docs/swagger.json" + - name: cm-beetle + image: ghcr.io/cloud-barista/cm-beetle:latest + ports: + - containerPort: 8056 + env: + - name: BEETLE_ROOT + value: "/app" + - name: CBLOG_ROOT + value: "/app" + - name: CBLOG_ROOT + value: "/app" + - name: TUMBLEBUG_CALL_METHOD + value: "REST" + - name: TUMBLEBUG_REST_URL + value: "http://localhost:1323/tumblebug" + - name: API_USERNAME + value: "default" + - name: API_PASSWORD + value: "default" + - name: AUTOCONTROL_DURATION_MS + value: "10000" + - name: SELF_ENDPOINT + value: "localhost:8056" + - name: API_DOC_PATH + value: "/app/api/swagger.json" diff --git a/pkg/config/README.md b/pkg/config/README.md index fad316f..9037420 100644 --- a/pkg/config/README.md +++ b/pkg/config/README.md @@ -1,8 +1,9 @@ ### Config Package #### Overview + The `config` package manages configurations in Go applications, -ensuring compatibility between `config.yaml` and `setup.env`. +ensuring compatibility between `config.yaml` and `setup.env`. `setup.env` is used to setup environment variables. Note - When both environment variables and config.yaml settings are present, @@ -13,15 +14,16 @@ the package prioritizes environment variables, overriding equivalent settings in The below configurations are compatible in this project. - `setup.env` contains: - ``` - export LOGFILE_PATH=cm-beetle.log - ``` + + ``` + export LOGFILE_PATH=beetle.log + ``` - `config.yaml` has: - ```yaml - logfile: - path: ./cm-beetle.log - ``` + ```yaml + logfile: + path: ./beetle.log + ``` #### How to use it diff --git a/pkg/config/config.go b/pkg/config/config.go index 87947b7..402e159 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -47,7 +47,7 @@ func Init() { viper.AutomaticEnv() // Values set in runtime - if viper.GetString("cmbeetle.root") == "" { + if viper.GetString("beetle.root") == "" { fmt.Println("find project root by using project name") log.Println("find project root by using project name") @@ -69,10 +69,10 @@ func Init() { log.Printf("project root directory: %s\n", projectRoot) // Set the binary path - viper.Set("cmbeetle.root", projectRoot) + viper.Set("beetle.root", projectRoot) viper.Set("cbstore.root", projectRoot) viper.Set("cblog.root", projectRoot) - viper.Set("apidoc.path", projectRoot+"/pkg/api/rest/docs/swagger.json") + viper.Set("apidoc.path", projectRoot+"/api/swagger.json") } // Recursively print all keys and values in Viper diff --git a/pkg/core/common/config.go b/pkg/core/common/config.go index d954dab..59dadd6 100644 --- a/pkg/core/common/config.go +++ b/pkg/core/common/config.go @@ -221,13 +221,13 @@ func InitConfig(id string) error { DBUrl = NVL(os.Getenv("DB_URL"), "localhost:3306") fmt.Println(" " + DBUrl) case StrDBDatabase: - DBDatabase = NVL(os.Getenv("DB_DATABASE"), "cb_beetle") + DBDatabase = NVL(os.Getenv("DB_DATABASE"), "cm_beetle") fmt.Println(" " + DBDatabase) case StrDBUser: - DBUser = NVL(os.Getenv("DB_USER"), "cb_beetle") + DBUser = NVL(os.Getenv("DB_USER"), "cm_beetle") fmt.Println(" " + DBUser) case StrDBPassword: - DBPassword = NVL(os.Getenv("DB_PASSWORD"), "cb_beetle") + DBPassword = NVL(os.Getenv("DB_PASSWORD"), "cm_beetle") fmt.Println(" " + DBPassword) case StrAutocontrolDurationMs: AutocontrolDurationMs = NVL(os.Getenv("AUTOCONTROL_DURATION_MS"), "10000") diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 93542d5..47ab5f4 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -135,10 +135,10 @@ func getLogFileConfig() (string, int, int, int, bool) { // Set config values logFilePath := viper.GetString("logfile.path") - // Default: cm-beetle.log + // Default: beetle.log if logFilePath == "" { - log.Warn().Msg("LOGFILE_PATH is not set. Using default value: cm-beetle.log") - logFilePath = "cm-beetle.log" + log.Warn().Msg("LOGFILE_PATH is not set. Using default value: beetle.log") + logFilePath = "beetle.log" } // Default: 10 MB