Skip to content

Commit

Permalink
Merge pull request #74 from yunkon-kim/241021-19
Browse files Browse the repository at this point in the history
Upgrade OpenTofu and update Terrarium
  • Loading branch information
yunkon-kim authored Oct 21, 2024
2 parents 16ec48e + 49c46ab commit 87704f5
Show file tree
Hide file tree
Showing 38 changed files with 839 additions and 571 deletions.
49 changes: 29 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##############################################################

# Using a specific version of golang based on alpine for building the application
FROM golang:1.21.4-alpine AS builder
FROM golang:1.23.0-alpine AS builder

# Installing necessary packages
# sqlite-libs and sqlite-dev for SQLite support
Expand All @@ -30,7 +30,7 @@ RUN make prod
##############################################################

# Using the latest Ubuntu image for the production stage
FROM ubuntu:latest as prod
FROM ubuntu:22.04 AS prod

# Setting the working directory for the application
WORKDIR /app
Expand All @@ -46,29 +46,38 @@ COPY --from=builder /go/src/github.com/cloud-barista/mc-terrarium/scripts/ /app/
COPY --from=builder /go/src/github.com/cloud-barista/mc-terrarium/cmd/mc-terrarium/mc-terrarium /app/

RUN apt-get update && apt-get install -y git
RUN ./scripts/install-tofu-1.7.1.sh
RUN ./scripts/install-tofu.sh 1.8.3

# Setting various environment variables required by the application
ENV MCTERRARIUM_ROOT=/app \
LOGFILE_PATH=/app/.terrarium/mc-terrarium.log \
LOGFILE_MAXSIZE=10 \
ENV TERRARIUM_ROOT=/app

## Set SELF_ENDPOINT, to access Swagger API dashboard outside (Ex: export SELF_ENDPOINT=x.x.x.x:8056)
ENV TERRARIUM_SELF_ENDPOINT=localhost:8888

## 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 TERRARIUM_API_ALLOW_ORIGINS=* \
TERRARIUM_API_AUTH_ENABLED=true \
TERRARIUM_API_USERNAME=default \
TERRARIUM_API_PASSWORD=default

## Logger configuration
# Set log file path (default logfile path: ./log/terrarium.log)
# Set log level, such as trace, debug info, warn, error, fatal, and panic
ENV LOGFILE_PATH=/app/log/terrarium.log \
LOGFILE_MAXSIZE=1000 \
LOGFILE_MAXBACKUPS=3 \
LOGFILE_MAXAGE=30 \
LOGFILE_COMPRESS=false \
LOGLEVEL=debug \
LOGWRITER=both \
NODE_ENV=development \
DB_URL=localhost:3306 \
DB_DATABASE=poc_mc_net_tf \
DB_USER=poc_mc_net_tf \
DB_PASSWORD=poc_mc_net_tf \
API_ALLOW_ORIGINS=* \
API_AUTH_ENABLED=true \
API_USERNAME=default \
API_PASSWORD=default \
AUTOCONTROL_DURATION_MS=10000 \
SELF_ENDPOINT=localhost:8888 \
API_DOC_PATH=/app/pkg/api/rest/docs/swagger.json
LOGLEVEL=info \
LOGWRITER=both

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

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

# Setting the entrypoint for the application
ENTRYPOINT [ "/app/mc-terrarium" ]
Expand Down
105 changes: 89 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,96 @@
default:
cd cmd/mc-terrarium && $(MAKE)
# Makefile for MC-Terrarium in Cloud-Barista.

cc:
cd cmd/mc-terrarium && $(MAKE)
MODULE_NAME := mc-terrarium
PROJECT_NAME := github.com/cloud-barista/$(MODULE_NAME)
PKG_LIST := $(shell go list $(PROJECT_NAME)/... 2>&1)

run:
cd cmd/mc-terrarium && $(MAKE) run
GOPROXY_OPTION := GOPROXY=direct # default: GOPROXY=https://proxy.golang.org,direct
GO := $(GOPROXY_OPTION) go
GOPATH := $(shell go env GOPATH)
SWAG := ~/go/bin/swag

runwithport:
cd cmd/mc-terrarium && $(MAKE) runwithport --port=$(PORT)
.PHONY: all dependency lint update swag swagger build arm prod run stop clean help

clean:
cd cmd/mc-terrarium && $(MAKE) clean
all: swag build ## Default target: build the project

prod:
cd cmd/mc-terrarium && $(MAKE) prod
dependency: ## Get dependencies
@echo "Checking dependencies..."
@$(GO) mod tidy
@echo "Checked!"

source-model:
cd pkg/api/rest/model && $(MAKE) source-model
lint: dependency ## Lint the files
@echo "Running linter..."
@if [ ! -f "$(GOPATH)/bin/golangci-lint" ] && [ ! -f "$(shell go env GOROOT)/bin/golangci-lint" ]; then \
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.2; \
fi
@golangci-lint run -E contextcheck -D unused
@echo "Linter finished!"

swag swagger:
cd pkg/ && $(MAKE) swag
update: ## Update all module dependencies
@echo "Updating dependencies..."
@cd cmd/$(MODULE_NAME) && $(GO) get -u
@echo "Checking dependencies..."
@$(GO) mod tidy
@echo "Updated!"

swag swagger: ## Generate Swagger API documentation
@echo "Generating Swagger API documentation..."
@ln -sf cmd/$(MODULE_NAME)/main.go ./main.go
@$(SWAG) i --parseDependency --parseInternal --generalInfo ./main.go --dir ./ --output ./api
@rm ./main.go
@echo "Generated Swagger API documentation!"

# build: lint swag ## Build the binary file for amd64
build: ## Build the binary file for amd64
@echo "Building the binary for amd64..."
@cd cmd/$(MODULE_NAME) && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -o $(MODULE_NAME) main.go
@echo "Build finished!"

# arm: lint swag ## Build the binary file for ARM
arm: ## Build the binary file for ARM
@echo "Building the binary for ARM..."
@cd cmd/$(MODULE_NAME) && CGO_ENABLED=0 GOOS=linux GOARCH=arm $(GO) build -o $(MODULE_NAME)-arm main.go
@echo "Build finished!"

# prod: lint swag ## Build the binary file for production
prod: ## Build the binary file for production
@echo "Building the binary for amd64 production..."
# Note - Using cgo write normal Go code that imports a pseudo-package "C". I may not need on cross-compiling.
# Note - You can find possible platforms by 'go tool dist list' for GOOS and GOARCH
# Note - Using the -ldflags parameter can help set variable values at compile time.
# Note - Using the -s and -w linker flags can strip the debugging information.
@cd cmd/$(MODULE_NAME) && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GO) build -ldflags '-s -w' -tags $(MODULE_NAME) -v -o $(MODULE_NAME) main.go
@echo "Build finished!"

run: build ## Run the built binary
@echo "Running the binary..."
@source conf/setup.env; \
cd cmd/$(MODULE_NAME) && \
(./$(MODULE_NAME) || { echo "Trying with sudo..."; sudo ./$(MODULE_NAME); })

stop: ## Stop the built binary
@echo "Stopping the binary..."
@sudo killall $(MODULE_NAME) 2>/dev/null || true
@echo "Stopped!"

clean: ## Remove previous build
@echo "Cleaning build..."
@rm -f coverage.out
@rm -f api/docs.go api/swagger.*
@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-down: ## Down services by docker compose
# @echo "Removing services by docker compose..."
# @cd deployments/docker-compose && docker compose down

help: ## Display this help screen
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ chmod +x install-opentofu.sh
./install-opentofu.sh --install-method deb

# Remove the installer:
rm install-opentofu.sh
rm -f install-opentofu.sh
```

#### Get source code
Expand Down
11 changes: 5 additions & 6 deletions pkg/api/rest/docs/docs.go → api/docs.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs
// Package api Code generated by swaggo/swag. DO NOT EDIT
package api

import "github.com/swaggo/swag"

Expand All @@ -11,8 +11,7 @@ const docTemplate = `{
"title": "{{.Title}}",
"contact": {
"name": "API Support",
"url": "http://cloud-barista.github.io",
"email": "contact-to-cloud-barista@googlegroups.com"
"url": "https://github.com/cloud-barista/mc-terrarium/issues/new"
},
"license": {
"name": "Apache 2.0",
Expand Down Expand Up @@ -2060,10 +2059,10 @@ const docTemplate = `{
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "latest",
Host: "",
Host: "localhost:8888",
BasePath: "/terrarium",
Schemes: []string{},
Title: "Multi-Cloud Terrarium REST API",
Title: "Multi-Cloud Terrarium REST API",
Description: "Multi-Cloud Terrarium (mc-terrarium) aims to provide an environment to enrich multi-cloud infrastructure.",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/rest/docs/swagger.json → api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
"swagger": "2.0",
"info": {
"description": "Multi-Cloud Terrarium (mc-terrarium) aims to provide an environment to enrich multi-cloud infrastructure.",
"title": "Multi-Cloud Terrarium REST API",
"title": "Multi-Cloud Terrarium REST API",
"contact": {
"name": "API Support",
"url": "http://cloud-barista.github.io",
"email": "contact-to-cloud-barista@googlegroups.com"
"url": "https://github.com/cloud-barista/mc-terrarium/issues/new"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "latest"
},
"host": "localhost:8888",
"basePath": "/terrarium",
"paths": {
"/httpVersion": {
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/rest/docs/swagger.yaml → api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,17 @@ definitions:
example: asia-northeast3
type: string
type: object
host: localhost:8888
info:
contact:
email: contact-to-cloud-barista@googlegroups.com
name: API Support
url: http://cloud-barista.github.io
url: https://github.com/cloud-barista/mc-terrarium/issues/new
description: Multi-Cloud Terrarium (mc-terrarium) aims to provide an environment
to enrich multi-cloud infrastructure.
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
title: Multi-Cloud Terrarium REST API
title: Multi-Cloud Terrarium REST API
version: latest
paths:
/httpVersion:
Expand Down
95 changes: 59 additions & 36 deletions cmd/mc-terrarium/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,66 @@ import (
"sync"

// Black import (_) is for running a package's init() function without using its other contents.
_ "github.com/cloud-barista/mc-terrarium/pkg/config"
_ "github.com/cloud-barista/mc-terrarium/pkg/logger"
"github.com/cloud-barista/mc-terrarium/pkg/config"
"github.com/cloud-barista/mc-terrarium/pkg/logger"
"github.com/fsnotify/fsnotify"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

//_ "github.com/go-sql-driver/mysql"

// _ "github.com/mattn/go-sqlite3"

restServer "github.com/cloud-barista/mc-terrarium/pkg/api/rest/server"
restServer "github.com/cloud-barista/mc-terrarium/pkg/api/rest"

"github.com/cloud-barista/mc-terrarium/pkg/readyz"
)

// NoOpLogger is an implementation of resty.Logger that discards all logs.
type NoOpLogger struct{}

func (n *NoOpLogger) Errorf(format string, v ...interface{}) {}
func (n *NoOpLogger) Warnf(format string, v ...interface{}) {}
func (n *NoOpLogger) Debugf(format string, v ...interface{}) {}

func init() {
readyz.SetReady(false)

// Initialize the configuration from "config.yaml" file or environment variables
config.Init()

// Initialize the logger
logger := logger.NewLogger(logger.Config{
LogLevel: config.Terrarium.LogLevel,
LogWriter: config.Terrarium.LogWriter,
LogFilePath: config.Terrarium.LogFile.Path,
MaxSize: config.Terrarium.LogFile.MaxSize,
MaxBackups: config.Terrarium.LogFile.MaxBackups,
MaxAge: config.Terrarium.LogFile.MaxAge,
Compress: config.Terrarium.LogFile.Compress,
})

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

// @title Multi-Cloud Terrarium REST API
// @version latest
// @description Multi-Cloud Terrarium (mc-terrarium) aims to provide an environment to enrich multi-cloud infrastructure.

// @contact.name API Support
// @contact.url https://github.com/cloud-barista/mc-terrarium/issues/new

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host localhost:8888
// @BasePath /terrarium

// @securityDefinitions.basic BasicAuth
func main() {

log.Info().Msg("starting mc-terrarium server")
log.Info().Msg("preparing to run mc-terrarium server...")

// Set the default port number "8888" for the REST API server to listen on
port := flag.String("port", "8888", "port number for the restapiserver to listen to")
Expand All @@ -51,39 +91,22 @@ func main() {
}
log.Debug().Msgf("port number: %s", *port)

// 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/mcterrarium.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/mcterrarium.s3db")

// Watch config file changes
// go func() {
// viper.WatchConfig()
// viper.OnConfigChange(func(e fsnotify.Event) {
// log.Debug().Str("file", e.Name).Msg("config file changed")
// err := viper.ReadInConfig()
// if err != nil { // Handle errors reading the config file
// log.Fatal().Err(err).Msg("fatal error in config file")
// }
// err = viper.Unmarshal(&common.RuntimeConf)
// if err != nil {
// log.Panic().Err(err).Msg("error unmarshaling runtime configuration")
// }
// })
// }()
go func() {
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Debug().Str("file", e.Name).Msg("config file changed")
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
log.Fatal().Err(err).Msg("fatal error in config file")
}
err = viper.Unmarshal(&config.RuntimeConfig)
if err != nil {
log.Panic().Err(err).Msg("error unmarshaling runtime configuration")
}
config.Terrarium = config.RuntimeConfig.Terrarium
})
}()

// Launch API servers (REST)
wg := new(sync.WaitGroup)
Expand Down
Loading

0 comments on commit 87704f5

Please sign in to comment.