Skip to content

Commit

Permalink
bug fix: environment variables and configs
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Jun 10, 2024
2 parents 5a2d728 + d71b1b2 commit 6bcb219
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 91 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
6 changes: 3 additions & 3 deletions cmd/cm-beetle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions conf/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion conf/log_conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 11 additions & 11 deletions conf/setup.env
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
export APIDOC_PATH=$BEETLE_ROOT/api/swagger.json
22 changes: 11 additions & 11 deletions conf/template-config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
22 changes: 11 additions & 11 deletions conf/template-setup.env
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
export APIDOC_PATH=$BEETLE_ROOT/api/swagger.json
42 changes: 26 additions & 16 deletions deployments/kubernetes/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
18 changes: 10 additions & 8 deletions pkg/config/README.md
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/core/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ func InitConfig(id string) error {
DBUrl = NVL(os.Getenv("DB_URL"), "localhost:3306")
fmt.Println("<DB_URL> " + DBUrl)
case StrDBDatabase:
DBDatabase = NVL(os.Getenv("DB_DATABASE"), "cb_beetle")
DBDatabase = NVL(os.Getenv("DB_DATABASE"), "cm_beetle")
fmt.Println("<DB_DATABASE> " + DBDatabase)
case StrDBUser:
DBUser = NVL(os.Getenv("DB_USER"), "cb_beetle")
DBUser = NVL(os.Getenv("DB_USER"), "cm_beetle")
fmt.Println("<DB_USER> " + DBUser)
case StrDBPassword:
DBPassword = NVL(os.Getenv("DB_PASSWORD"), "cb_beetle")
DBPassword = NVL(os.Getenv("DB_PASSWORD"), "cm_beetle")
fmt.Println("<DB_PASSWORD> " + DBPassword)
case StrAutocontrolDurationMs:
AutocontrolDurationMs = NVL(os.Getenv("AUTOCONTROL_DURATION_MS"), "10000")
Expand Down
6 changes: 3 additions & 3 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6bcb219

Please sign in to comment.