Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update cost estimate api #18

Merged
merged 10 commits into from
Oct 30, 2024
712 changes: 528 additions & 184 deletions api/docs.go

Large diffs are not rendered by default.

712 changes: 528 additions & 184 deletions api/swagger.json

Large diffs are not rendered by default.

526 changes: 376 additions & 150 deletions api/swagger.yaml

Large diffs are not rendered by default.

74 changes: 62 additions & 12 deletions cmd/cm-ant/main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package main

import (
"log"
"fmt"

"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"

"github.com/cloud-barista/cm-ant/internal/app"
"github.com/cloud-barista/cm-ant/internal/config"
"github.com/cloud-barista/cm-ant/internal/utils"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

// InitRouter initializes the routing for CM-ANT API server.
Expand All @@ -17,55 +23,99 @@ import (
// @version 0.2.2
// @description CM-ANT REST API swagger document.
// @basePath /ant

type CallerHook struct{}

func (h CallerHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
if pc, file, line, ok := runtime.Caller(3); ok {
shortFile := file[strings.LastIndex(file, "/")+1:]
e.Str("file", fmt.Sprintf("%s:%d", shortFile, line))
funcName := strings.Replace(runtime.FuncForPC(pc).Name(), "github.com/cloud-barista/", "", 1)
e.Str("func", funcName)
}
}

func main() {
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
level := strings.ToUpper(fmt.Sprintf("%s", i))
switch level {
case "DEBUG":
return fmt.Sprintf("\033[36m| %-6s|\033[0m", level) // Cyan
case "INFO":
return fmt.Sprintf("\033[32m| %-6s|\033[0m", level) // Green
case "WARN":
return fmt.Sprintf("\033[33m| %-6s|\033[0m", level) // Yellow
case "ERROR":
return fmt.Sprintf("\033[31m| %-6s|\033[0m", level) // Red
case "FATAL":
return fmt.Sprintf("\033[35m| %-6s|\033[0m", level) // Magenta
default:
return fmt.Sprintf("| %-6s|", level) // Default color
}
}
output.FormatMessage = func(i interface{}) string {
if i == nil {
return ""
}
return fmt.Sprintf("message: \033[1m%s\033[0m", i)
}

output.FormatFieldName = func(i interface{}) string {
return fmt.Sprintf("%s:", i)
}
output.FormatFieldValue = func(i interface{}) string {
return fmt.Sprintf("\033[1m%s\033[0m", i)
}

log.Logger = zerolog.New(output).With().Timestamp().Logger().Hook(CallerHook{})

err := utils.Script(utils.JoinRootPathWith("/script/install_required_utils.sh"), []string{})
if err != nil {
log.Fatal("required tool can not install")
log.Fatal().Msg("required tool can not install")
}
utils.LogInfo("Starting CM-Ant server initialization...")

// Initialize the configuration for CM-Ant server
err = config.InitConfig()
if err != nil {
log.Fatalf("[ERROR] CM-Ant server config error: %v", err)
log.Fatal().Msgf("CM-Ant server config error: %v", err)
}

// Create a new instance of the CM-Ant server
s, err := app.NewAntServer()
if err != nil {
log.Fatalf("[ERROR] CM-Ant server creation error: %v", err)
log.Fatal().Msgf("CM-Ant server creation error: %v", err)
}

// Initialize the router for the CM-Ant server
err = s.InitRouter()
if err != nil {
log.Fatalf("[ERROR] CM-Ant server init router error: %v", err)
log.Fatal().Msgf("CM-Ant server init router error: %v", err)
}

utils.LogInfo("CM-Ant server initialization completed successfully.")
log.Info().Msgf("CM-Ant server initialization completed successfully.")

// Create a channel to listen for OS signals
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

utils.LogInfo("Starting the CM-Ant server...")
log.Info().Msgf("Starting the CM-Ant server...")
go func() {
if err := s.Start(); err != nil {
log.Fatalf("[ERROR] CM-Ant start server error: %v", err)
log.Fatal().Msgf("CM-Ant start server error: %v", err)
}
}()

utils.LogInfo("CM-Ant server started successfully. Waiting for termination signal...")
log.Info().Msgf("CM-Ant server started successfully. Waiting for termination signal...")

// Wait for termination signal
<-stop

utils.LogInfo("Shutting down CM-Ant server...")
log.Info().Msgf("Shutting down CM-Ant server...")

// Perform any necessary cleanup actions here, such as closing connections or saving state.
// Optionally wait for pending operations to complete gracefully.

utils.LogInfo("CM-Ant server stopped gracefully.")
log.Info().Msgf("CM-Ant server stopped gracefully.")
os.Exit(0)
}
9 changes: 5 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
root:
path:

server:
port: 8880

Expand All @@ -16,13 +13,17 @@ tumblebug:
username: default
password: default

cost:
estimation:
updateInterval: "168h"

load:
retry: 2
jmeter:
dir: "/opt/ant/jmeter"
version: 5.6

logging:
log:
level: info

database:
Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ services:
restart: unless-stopped

ant-postgres:
container_name: ant-postgres
image: timescale/timescaledb:latest-pg16
ports:
- "5432:5432"
Expand All @@ -50,7 +51,7 @@ services:
restart: unless-stopped

cb-tumblebug:
image: cloudbaristaorg/cb-tumblebug:0.9.13
image: cloudbaristaorg/cb-tumblebug:0.9.21
container_name: cb-tumblebug
platform: linux/amd64
ports:
Expand Down Expand Up @@ -147,7 +148,7 @@ services:
restart: unless-stopped

cb-spider:
image: cloudbaristaorg/cb-spider:0.9.4
image: cloudbaristaorg/cb-spider:0.9.8
container_name: cb-spider
platform: linux/amd64
networks:
Expand Down
Loading