Skip to content

Commit

Permalink
Enhance to run Beetle server
Browse files Browse the repository at this point in the history
* Update to be preparing and running Beetle server
* Update Dockerfile and docker-compose.yaml
* Copy cloud_conf.yaml, which is required to run the Tumblebug container
  • Loading branch information
yunkon-kim committed Sep 6, 2024
1 parent cc48b60 commit adb5636
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 221 deletions.
45 changes: 22 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ RUN --mount=type=cache,target=/go/pkg/mod \
# Copy some necessary files to the container
COPY api ./api
COPY cmd/cm-beetle ./cmd/cm-beetle
COPY conf ./conf
COPY pkg ./pkg
COPY scripts ./scripts
# COPY conf ./conf

# Build the Go app
RUN --mount=type=cache,target=/go/pkg/mod \
Expand All @@ -46,17 +46,27 @@ RUN apt-get update && apt-get install -y --no-install-recommends \

## Copy the Pre-built binary and necessary files from the previous stage
COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/scripts/ /app/scripts/
COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/conf/ /app/conf/
COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/cmd/cm-beetle/cm-beetle /app/
COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/api/ /app/api/
# COPY --from=builder /go/src/github.com/cloud-barista/cm-beetle/conf/ /app/conf/

## Set environment variables
# Set system endpoints
ENV BEETLE_ROOT=/app \
BEETLE_CBSTORE_ROOT=/app \
BEETLE_CBLOG_ROOT=/app
ENV BEETLE_ROOT=/app

ENV BEETLE_TUMBLEBUG_REST_URL=http://localhost:1323/tumblebug
## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
ENV BEETLE_SELF_ENDPOINT=localhost:8056

## Set API access config
# API_ALLOW_ORIGINS (ex: https://cloud-barista.org,xxx.xxx.xxx.xxx or * for all)
# Set ENABLE_AUTH=true currently for basic auth for all routes (i.e., url or path)
ENV BEETLE_API_ALLOW_ORIGINS=* \
BEETLE_API_AUTH_ENABLED=true \
BEETLE_API_USERNAME=default \
BEETLE_API_PASSWORD=default

## Set internal DB config (lkvstore: local key-value store, default file path: ./db/beetle.db)
ENV BEETLE_LKVSTORE_PATH=/app/db/beetle.db

## Logger configuration
# Set log file path (default logfile path: ./beetle.log)
Expand All @@ -66,30 +76,19 @@ ENV BEETLE_LOGFILE_PATH=/app/log/beetle.log \
BEETLE_LOGFILE_MAXBACKUPS=3 \
BEETLE_LOGFILE_MAXAGE=30 \
BEETLE_LOGFILE_COMPRESS=false \
BEETLE_LOGLEVEL=info
BEETLE_LOGLEVEL=info \
BEETLE_LOGWRITER=both

# Set execution environment, such as development or production
ENV BEETLE_NODE_ENV=production

## Set internal DB config (SQLlite)
ENV BEETLE_SQLITE_URL=localhost:3306 \
BEETLE_SQLITE_DATABASE=cm_beetle \
BEETLE_SQLITE_USER=cm_beetle \
BEETLE_SQLITE_PASSWORD=cm_beetle

## Set API access config
# API_ALLOW_ORIGINS (ex: https://cloud-barista.org,xxx.xxx.xxx.xxx or * for all)
# Set ENABLE_AUTH=true currently for basic auth for all routes (i.e., url or path)
ENV BEETLE_API_ALLOW_ORIGINS=* \
BEETLE_API_AUTH_ENABLED=true \
BEETLE_API_USERNAME=default \
BEETLE_API_PASSWORD=default

## Set period for auto control goroutine invocation
ENV BEETLE_AUTOCONTROL_DURATION_MS=10000

## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
ENV BEETLE_SELF_ENDPOINT=localhost:8056
## Set Tumblebug access config
ENV BEETLE_TUMBLEBUG_REST_URL=http://localhost:1323 \
BEETLE_TUMBLEBUG_API_USERNAME=default \
BEETLE_TUMBLEBUG_API_PASSWORD=default

ENTRYPOINT [ "/app/cm-beetle" ]

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ clean: ## Remove previous build
@cd cmd/$(MODULE_NAME) && $(GO) clean
@echo "Cleaned!"

compose: swag ## Build and up services by docker compose
@echo "Building and starting services by docker compose..."
@cd deployments/docker-compose && DOCKER_BUILDKIT=1 docker compose up --build

compose-up: ## Up services by docker compose
@echo "Starting services by docker compose..."
@cd deployments/docker-compose && docker compose up

compose-build-up: swag ## Build and up services by docker compose
@echo "Building and starting services by docker compose..."
@cd deployments/docker-compose && DOCKER_BUILDKIT=1 docker compose up --build

compose-down: ## Down services by docker compose
@echo "Removing services by docker compose..."
@cd deployments/docker-compose && docker compose down
Expand Down
78 changes: 38 additions & 40 deletions cmd/cm-beetle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package main

import (
"flag"
"os"
"strconv"
"sync"
"time"

"github.com/cloud-barista/cm-beetle/pkg/config"
"github.com/cloud-barista/cm-beetle/pkg/lkvstore"
"github.com/cloud-barista/cm-beetle/pkg/logger"
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -52,22 +52,27 @@ func init() {

// Initialize the logger
logger := logger.NewLogger(logger.Config{
LogLevel: viper.GetString("beetle.loglevel"),
LogWriter: viper.GetString("beetle.logwriter"),
LogFilePath: viper.GetString("beetle.logfile.path"),
MaxSize: viper.GetInt("beetle.logfile.maxsize"),
MaxBackups: viper.GetInt("beetle.logfile.maxbackups"),
MaxAge: viper.GetInt("beetle.logfile.maxage"),
Compress: viper.GetBool("beetle.logfile.compress"),
LogLevel: config.Beetle.LogLevel,
LogWriter: config.Beetle.LogWriter,
LogFilePath: config.Beetle.LogFile.Path,
MaxSize: config.Beetle.LogFile.MaxSize,
MaxBackups: config.Beetle.LogFile.MaxBackups,
MaxAge: config.Beetle.LogFile.MaxAge,
Compress: config.Beetle.LogFile.Compress,
})

// Set the global logger
log.Logger = *logger

// Initialize the local key-value store with the specified file path
dbFilePath := config.Beetle.LKVStore.Path
lkvstore.Init(lkvstore.Config{
DbFilePath: dbFilePath,
})

// Check Tumblebug readiness
tumblebugEp := viper.GetString("beetle.tumblebug.endpoint")
url := tumblebugEp + "/tumblebug/readyz"
isReady, err := checkReadiness(url)
apiUrl := config.Tumblebug.RestUrl + "/readyz"
isReady, err := checkReadiness(apiUrl)

if err != nil || !isReady {
log.Fatal().Err(err).Msg("Tumblebug is not ready. Exiting...")
Expand Down Expand Up @@ -139,7 +144,24 @@ func checkReadiness(url string) (bool, error) {

func main() {

log.Info().Msg("CM-Beetle server is starting...")
log.Info().Msg("preparing to run CM-Beetle server...")

// Load the state from the file back into the key-value store
if err := lkvstore.LoadLkvStore(); err != nil {
log.Error().Err(err).Msgf("error loading data from the lkvstore (file).")
}

log.Info().Msg("successfully load data from the lkvstore (file).")

defer func() {
// Save the current state of the key-value store to file
if err := lkvstore.SaveLkvStore(); err != nil {
log.Error().Err(err).Msgf("error saving data to the lkvstore (file).")
}
log.Info().Msg("successfully save data to the lkvstore (file).")
}()

log.Info().Msg("Setting CM-Beetle REST API server...")

// Set the default port number "8056" for the REST API server to listen on
port := flag.String("port", "8056", "port number for the restapiserver to listen to")
Expand All @@ -151,33 +173,6 @@ func main() {
}
log.Debug().Msgf("port number: %s", *port)

// common.SpiderRestUrl = common.NVL(os.Getenv("BEETLE_SPIDER_REST_URL"), "http://localhost:1024/spider")
// common.DragonflyRestUrl = common.NVL(os.Getenv("BEETLE_DRAGONFLY_REST_URL"), "http://localhost:9090/dragonfly")
common.TumblebugRestUrl = common.NVL(os.Getenv("BEETLE_TUMBLEBUG_ENDPOINT"), "http://localhost:1323") + "/tumblebug"
common.DBUrl = common.NVL(os.Getenv("BEETLE_SQLITE_URL"), "localhost:3306")
common.DBDatabase = common.NVL(os.Getenv("BEETLE_SQLITE_DATABASE"), "cm_beetle")
common.DBUser = common.NVL(os.Getenv("BEETLE_SQLITE_USER"), "cm_beetle")
common.DBPassword = common.NVL(os.Getenv("BEETLE_SQLITE_PASSWORD"), "cm_beetle")
common.AutocontrolDurationMs = common.NVL(os.Getenv("BEETLE_AUTOCONTROL_DURATION_MS"), "10000")

// load the latest configuration from DB (if exist)
// fmt.Println("")
// fmt.Println("[Update system environment]")
// common.UpdateGlobalVariable(common.StrDragonflyRestUrl)
// common.UpdateGlobalVariable(common.StrSpiderRestUrl)
// common.UpdateGlobalVariable(common.StrAutocontrolDurationMs)

// load config
//masterConfigInfos = confighandler.GetMasterConfigInfos()

//Setup database (meta_db/dat/cmbeetle.s3db)
log.Info().Msg("setting SQL Database")
err := os.MkdirAll("./meta_db/dat/", os.ModePerm)
if err != nil {
log.Error().Err(err).Msg("error creating directory")
}
log.Debug().Msgf("database file path: %s", "./meta_db/dat/cmbeetle.s3db")

// Watch config file changes
go func() {
viper.WatchConfig()
Expand All @@ -187,10 +182,13 @@ func main() {
if err != nil { // Handle errors reading the config file
log.Fatal().Err(err).Msg("fatal error in config file")
}
err = viper.Unmarshal(&common.RuntimeConf)
err = viper.Unmarshal(&config.RuntimeConfig)
if err != nil {
log.Panic().Err(err).Msg("error unmarshaling runtime configuration")
}
config.Beetle = config.RuntimeConfig.Beetle
config.Beetle.Tumblebug.RestUrl = config.Beetle.Tumblebug.Endpoint + "/tumblebug"
config.Tumblebug = config.Beetle.Tumblebug
})
}()

Expand Down
96 changes: 96 additions & 0 deletions deployments/docker-compose/cb-tumblebug/conf/cloud_conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
cloud:
common:
enable: "y"
nlb:
enable: "y"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "y"
aws:
enable: "y"
nlb:
enable: "y"
interval: "10"
timeout: "-1"
threshold: "3"
k8scluster:
enable: "n"
azure:
enable: "y"
nlb:
enable: "y"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "y"
gcp:
enable: "y"
nlb:
enable: "y"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "n"
alibaba:
enable: "y"
nlb:
enable: "y"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "y"
tencent:
enable: "y"
nlb:
enable: "y"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "y"
ibm:
enable: "y"
nlb:
enable: "y"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "n"
nhncloud:
enable: "y"
k8scluster:
enable: "y"
openstack:
enable: "y"
nlb:
enable: "n"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "n"
cloudit :
enable: "y"
nlb:
enable: "n"
interval: "10"
timeout: "9"
threshold: "3"
k8scluster:
enable: "n"
nlbsw:
sw: "HAProxy"
version: "latest"
commandNlbPrepare: "wget https://raw.githubusercontent.com/cloud-barista/cb-tumblebug/main/scripts/nlb/deployNlb.sh; wget https://raw.githubusercontent.com/cloud-barista/cb-tumblebug/main/scripts/nlb/addTargetNode.sh; wget https://raw.githubusercontent.com/cloud-barista/cb-tumblebug/main/scripts/nlb/applyConfig.sh; chmod +x ~/deployNlb.sh ~/addTargetNode.sh ~/applyConfig.sh"
commandNlbDeploy: "sudo ~/deployNlb.sh"
commandNlbAddTargetNode: "sudo ~/addTargetNode.sh"
commandNlbApplyConfig: "sudo ~/applyConfig.sh"
nlbMcisCommonSpec: "aws-ap-northeast-2-t2-small"
nlbMcisCommonImage: "ubuntu18.04"
nlbMcisSubGroupSize: "2"
Loading

0 comments on commit adb5636

Please sign in to comment.