Skip to content

Commit

Permalink
Add environment config options (#3)
Browse files Browse the repository at this point in the history
* Update from main

* Add basic docker compose config

* Implement env parser

* Implement config load for hadesAPI

* Implement config load for hadesScheduler

* Add env variables

* Add API port to env variables

* Remove hardcoded queue value in API

* Improve log messages
  • Loading branch information
Mtze authored Sep 22, 2023
1 parent 946342f commit 99c3606
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RABBITMQ_URL=localhost:5672
RABBITMQ_DEFAULT_USER=admin
RABBITMQ_DEFAULT_PASS=admin
2 changes: 2 additions & 0 deletions HadesAPI/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ replace github.com/Mtze/HadesCI/shared => ../shared

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/caarlos0/env/v9 v9.0.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down
4 changes: 4 additions & 0 deletions HadesAPI/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
Expand All @@ -27,6 +29,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
33 changes: 21 additions & 12 deletions HadesAPI/main.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
package main

import (
"os"
"fmt"

"github.com/Mtze/HadesCI/shared/queue"
"github.com/Mtze/HadesCI/shared/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

var BuildQueue *queue.Queue

type HadesAPIConfig struct {
APIPort uint `env:"API_PORT,notEmpty" envDefault:"8080"`
RabbitMQConfig utils.RabbitMQConfig
}

func main() {
if is_debug := os.Getenv("DEBUG"); is_debug == "true" {
log.SetLevel(log.DebugLevel)
log.Warn("DEBUG MODE ENABLED")
}

// var err error
// BuildQueue, err = queue.Init("builds", "amqp://admin:admin@localhost:5672/")
// if err != nil {
// log.Panic(err)
// }
var cfg HadesAPIConfig
utils.LoadConfig(&cfg)

log.Info("Starting HadesAPI")
var err error
rabbitmqURL := fmt.Sprintf("amqp://%s:%s@%s/", cfg.RabbitMQConfig.User, cfg.RabbitMQConfig.Password, cfg.RabbitMQConfig.Url)
log.Debug("Connecting to RabbitMQ: ", rabbitmqURL)
BuildQueue, err = queue.Init("builds", rabbitmqURL)
if err != nil {
log.Panic(err)
}

log.Infof("Starting HadesAPI on port %d", cfg.APIPort)
gin.SetMode(gin.ReleaseMode)

r := gin.Default()
r.GET("/ping", ping)
r.POST("/build", AddBuildToQueue)
log.Panic(r.Run(":8080")) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")

log.Panic(r.Run(fmt.Sprintf(":%d", cfg.APIPort)))
}
13 changes: 7 additions & 6 deletions HadesScheduler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/client-go/tools/clientcmd"

"github.com/Mtze/HadesCI/shared/queue"
"github.com/Mtze/HadesCI/shared/utils"

amqp "github.com/rabbitmq/amqp091-go"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -48,14 +49,14 @@ func initializeKubeconfig() *kubernetes.Clientset {
}

func main() {

if is_debug := os.Getenv("DEBUG"); is_debug == "true" {
log.SetLevel(log.DebugLevel)
log.Warn("DEBUG MODE ENABLED")
}
var cfg utils.RabbitMQConfig
utils.LoadConfig(&cfg)

var err error
BuildQueue, err = queue.Init("builds", "amqp://admin:admin@localhost:5672/")
rabbitmqURL := fmt.Sprintf("amqp://%s:%s@%s/", cfg.User, cfg.Password, cfg.Url)
log.Debug("Connecting to RabbitMQ: ", rabbitmqURL)
BuildQueue, err = queue.Init("builds", rabbitmqURL)

if err != nil {
log.Panic(err)
}
Expand Down
31 changes: 30 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,37 @@ services:
dockerfile: ./HadesAPI/Dockerfile
ports:
- "8080:8080"
networks:
- hades
depends_on:
- rabbitmq
environment:
- RABBITMQ_URL=rabbitmq:5672

hadesScheduler:
image: hades-scheduler
build:
context: .
dockerfile: ./HadesScheduler/Dockerfile
dockerfile: ./HadesScheduler/Dockerfile
networks:
- hades
depends_on:
- rabbitmq
environment:
- RABBITMQ_URL=rabbitmq:5672

rabbitmq:
container_name: rabbitmq
image: rabbitmq:3-management
ports:
- "15672:15672"
- "5672:5672"
networks:
- hades
healthcheck:
test: ["CMD", "rabbitmq-diagnostics", "ping"]
interval: 30s
timeout: 10s
retries: 5
networks:
hades:
35 changes: 35 additions & 0 deletions shared/utils/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package utils

import (
"os"

"github.com/caarlos0/env/v9"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)

type RabbitMQConfig struct {
Url string `env:"RABBITMQ_URL,notEmpty"`
User string `env:"RABBITMQ_DEFAULT_USER,notEmpty"`
Password string `env:"RABBITMQ_DEFAULT_PASS,notEmpty"`
}

func LoadConfig(cfg interface{}) {

if is_debug := os.Getenv("DEBUG"); is_debug == "true" {
log.SetLevel(log.DebugLevel)
log.Warn("DEBUG MODE ENABLED")
}

err := godotenv.Load()
if err != nil {
log.WithError(err).Warn("Error loading .env file")
}

err = env.Parse(cfg)
if err != nil {
log.WithError(err).Fatal("Error parsing environment variables")
}

log.Debug("Config loaded: ", cfg)
}

0 comments on commit 99c3606

Please sign in to comment.