11 Commits

13 changed files with 208 additions and 115 deletions

View File

@@ -1,61 +1,31 @@
FROM golang as builder
FROM golang:alpine as builder
LABEL maintainer="Valentin KIVACHUK BURDA"
WORKDIR /app
ENV DOCKER_ENVIRONMENT=true
ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GO111MODULE=on
EXPOSE 49618
WORKDIR /go/src/oc-catalog
#######################################################
COPY go.mod .
COPY go.sum .
COPY . .
RUN go get github.com/beego/bee/v2 && go install github.com/beego/bee/v2@master
# Manually download swagger during build
RUN ["/bin/bash", "-c", \
"set -eo pipefail; \
mkdir -p swagger; \
curl -sL https://github.com/beego/swagger/archive/v3.tar.gz | tar xvvvz --overwrite -C swagger --strip-components=1"]
# Generating routers/commentsRouter.go
RUN bee generate routers
# Generating the swagger
RUN timeout 20 bee run -gendoc=true -downdoc=true -runmode=dev || :
RUN go mod download -x
RUN sed -i 's/http:\/\/127.0.0.1:8080\/swagger\/swagger.json/swagger.json/g' swagger/index.html
RUN sed -i 's/https:\/\/petstore.swagger.io\/v2\/swagger.json/swagger.json/g' swagger/index.html
# COPY . .
COPY main.go go.mod go.sum ./
RUN ls -l routers
COPY controllers controllers
COPY models models
COPY routers routers
COPY selfapi selfapi
COPY services services
COPY conf conf
COPY scripts scripts
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" .
# RUN go build -a -tags netgo -ldflags '-w -extldflags "-static"' -installsuffix cgo .
FROM scratch
RUN bee generate docs
WORKDIR /app
# COPY . .
COPY ./docker_catalog.json /etc/oc/catalog.json
COPY conf/app.conf /app/conf/
COPY --from=builder /app/oc-catalog /usr/bin/
COPY --from=builder /app/swagger /app/swagger
# FROM golang
# WORKDIR /go/src/oc-catalog
# COPY --from=builder /go/src/oc-catalog .
ENV DOCKER_ENVIRONMENT=true
RUN go build .
# UglyFix: Generate comments from swagger
RUN timeout 10 bee run -runargs test || exit 0
CMD [ "bee", "run", "-gendoc=true" ]
ENTRYPOINT ["oc-catalog"]

View File

@@ -8,12 +8,30 @@ To install the Beego bee command :
go install github.com/beego/bee/v2@master
To build and run :
## Running the app locally
In order to run the application we need to correct some errors that beego generates.
go mod tidy
bee run -downdoc=true -gendoc=true
**Router generation**
## Full deploy
Beego generates the routers from the comment of the controllers methods. However, there seems to be some errors when we let beego generate th routers from `bee run`.
We need to use `bee generate routers` to have a working router file.
**Swagger generation**
Using `bee run -downdoc=true -gendoc=true` beego download a swagger template and generate the JSON file (swagger.json) in `swagger/`. However the swagger template in `swagger/index.html` does not change the exemple's url for our current JSON file. When running locally we have to edit the call to construct the object holding the swagger information, with the parameter url set with the relative path to our JSON swagger file :
```
const ui = SwaggerUIBundle({
url: "swagger.json",
...
...
});
```
This issue is fixed in the Dockerfile build with two `sed` to change the value of the swagger file URL.
## Deployment with Docker compose
Deploy with docker:
`docker-compose -f docker-compose.yml -f docker-compose.backend.yml up --build`
@@ -23,15 +41,22 @@ and populate DB (or other scripts) with:
or out of docker `./scripts/populate_models.sh ./scripts/demo.json`
## Dev
### Dev
Start DB with `docker-compose up -d` and run the API with `bee run -downdoc=true -gendoc=true`
- Start DB with `docker-compose up -d`
- Run the API with `bee run -downdoc=true -gendoc=true`
## Multinode
### Multinode
Deploy :
Deploy
`docker-compose -f docker-compose.yml -f docker-compose.backend.yml -f docker-compose.multi.yml up --build`
Populate
`./scripts/multinode.sh ./scripts/demo.json`
## Populating the MongoDB database
From the root of the projet run :
`./scripts/multinode.sh ./scripts/demo.json`
This script should be updated to be ran from anywhere.

View File

@@ -4,14 +4,14 @@ runmode = dev
autorender = false
copyrequestbody = true
EnableDocs = true
EnableDocs = false
SessionOn = true
DCNAME = "DC_myDC"
DBPOINT = "demo_06042021"
# DCNAME = "DC_myDC"
# DBPOINT = "demo_06042021"
[mongodb]
url = mongodb://127.0.0.1:27017/beego-demo
# [mongodb]
# url = mongodb://127.0.0.1:27017/beego-demo
[mongodb_docker]
url = mongodb://mongo:27017/beego-demo
# [mongodb_docker]
# url = mongodb://mongo:27017/beego-demo

19
conf/conf.go Normal file
View File

@@ -0,0 +1,19 @@
package conf
import "sync"
type Config struct {
MongoURL string
DCNAME string
DBPOINT string
}
var instance *Config
var once sync.Once
func GetConfig() *Config {
once.Do(func() {
instance = &Config{}
})
return instance
}

5
conf_template.json Normal file
View File

@@ -0,0 +1,5 @@
{
"MongoURL" : "",
"DCNAME" : "",
"DBPOINT" : ""
}

View File

@@ -2,14 +2,19 @@ version: '3.4'
services:
oc-catalog:
build: .
image: oc-catalog:latest
container_name: oc-catalog
restart: always
environment:
- DOCKER_DCNAME=DC_myDC
depends_on:
- mongo
# depends_on:
# - mongo
networks:
- catalog
ports:
- 49618:49618
- 49618:49618
networks:
catalog:
name: catalog
external: true

View File

@@ -21,6 +21,9 @@ services:
- catalog
ports:
- 8081:8081
environment:
- ME_CONFIG_BASICAUTH_USERNAME=test
- ME_CONFIG_BASICAUTH_PASSWORD=test
volumes:
oc-catalog-data:

5
docker_catalog.json Normal file
View File

@@ -0,0 +1,5 @@
{
"MongoURL" : "mongodb://mongo:27017/",
"DCNAME" : "DC_myDC",
"DBPOINT" : "demo_06042021"
}

5
local_catalog.json Normal file
View File

@@ -0,0 +1,5 @@
{
"MongoURL" : "mongodb://127.0.0.1:27017",
"DCNAME" : "DC_myDC",
"DBPOINT" : "demo_06042021"
}

57
main.go
View File

@@ -3,12 +3,18 @@ package main
import (
"os"
"cloud.o-forge.io/core/oc-catalog/conf"
"cloud.o-forge.io/core/oc-catalog/routers"
"cloud.o-forge.io/core/oc-catalog/services"
"github.com/beego/beego/logs"
beego "github.com/beego/beego/v2/server/web"
"github.com/goraz/onion"
)
const defaultConfigFile = "/etc/oc/catalog.json"
const localConfigFile = "./local_catalog.json"
func main() {
// If we have any parameter, we run the beego directly
@@ -16,16 +22,59 @@ func main() {
beego.Run()
}
loadConfig()
routers.Init()
services.Init()
beego.BConfig.RunMode = "dev"
// if beego.BConfig.RunMode == "dev" {
// // beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
// }
if beego.BConfig.RunMode == "dev" {
// beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.Run()
defer func() {
services.MongoDisconnect()
}()
}
func loadConfig(){
log := logs.NewLogger(10000)
log.SetLogger("console")
configFile := ""
var o *onion.Onion
l3 := onion.NewEnvLayerPrefix("_", "OCCATALOG")
l2, err := onion.NewFileLayer(defaultConfigFile, nil)
if err == nil {
logs.Info("Config file found : " + defaultConfigFile)
configFile = defaultConfigFile
}
l1, err := onion.NewFileLayer(localConfigFile, nil)
if err == nil {
logs.Info("Local config file found " + localConfigFile + ", overriding default file")
configFile = localConfigFile
}
if configFile == "" {
logs.Info("No config file found, using env")
o = onion.New(l3)
} else if l1 == nil && l2 == nil {
o = onion.New(l1, l2, l3)
} else if l1 == nil {
o = onion.New(l2, l3)
} else if l2 == nil {
o = onion.New(l1, l3)
}
conf.GetConfig().MongoURL = o.GetStringDefault("MongoURL", "mongodb://127.0.0.1:27017")
conf.GetConfig().DCNAME = o.GetStringDefault("DCNAME", "DC_myDC")
conf.GetConfig().DBPOINT = o.GetStringDefault("DBPOINT", "demo_06042021")
}

View File

@@ -17,8 +17,6 @@ import (
"github.com/beego/beego/v2/core/logs"
bee "github.com/beego/bee/v2/generate/swaggergen"
"github.com/beego/beego/v2/adapter/swagger"
beego "github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/context"
@@ -87,8 +85,8 @@ func Init() {
ctx.Output.Body([]byte(services.DC_NAME))
})
// Force regenerate swagger before consuming the data
bee.GenerateDocs(".")
// Force regenerate swagger before consuming the data
// bee.GenerateDocs(".")
// Open our jsonFile
swaggerSchemaPath := "swagger/swagger.json"
@@ -127,3 +125,4 @@ func Init() {
initAuthMiddleware()
initUglyFixes()
}

View File

@@ -1,28 +1,30 @@
package services
import (
"os"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"cloud.o-forge.io/core/oc-catalog/conf"
)
func Init() {
Discoveryinit() //First init DC name
// Discoveryinit() //First init DC name
var DBpoint string
var err error
// var DBpoint string
// var err error
DBpoint = os.Getenv("DOCKER_DBPOINT")
if len(DBpoint) == 0 {
DBpoint, err = beego.AppConfig.String("DBPOINT")
if err != nil {
logs.Critical("DBPOINT URI error: %v", err)
panic(err)
}
DCName := conf.GetConfig().DCNAME
DBPoint := conf.GetConfig().DBPOINT
}
// DBpoint = os.Getenv("DOCKER_DBPOINT")
// if len(DBpoint) == 0 {
// DBpoint, err = beego.AppConfig.String("DBPOINT")
// if err != nil {
// logs.Critical("DBPOINT URI error: %v", err)
// panic(err)
// }
Mongoinit(DC_NAME + "-" + DBpoint)
// }
// Mongoinit(DC_NAME + "-" + DBpoint)
Mongoinit(DCName + "-" + DBPoint )
// Mongoinit("beego-demo")
}

View File

@@ -2,11 +2,10 @@ package services
import (
"context"
"os"
"time"
"cloud.o-forge.io/core/oc-catalog/conf"
"github.com/beego/beego/v2/core/logs"
beego "github.com/beego/beego/v2/server/web"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
@@ -56,43 +55,50 @@ func MongoDisconnect() {
func Mongoinit(DBname string) {
var baseConfig string
// var baseConfig string
var err error
MongoURL := conf.GetConfig().MongoURL
// if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 {
// baseConfig = "mongodb"
// } else {
// baseConfig = "mongodb_docker"
// }
if len(os.Getenv("DOCKER_ENVIRONMENT")) == 0 {
baseConfig = "mongodb"
} else {
baseConfig = "mongodb_docker"
}
mongoURI, err := beego.AppConfig.String(baseConfig + "::url")
if err != nil {
logs.Critical("MongoDB URI error: %v", err)
panic(err)
}
logs.Info("Connecting to %v", mongoURI)
clientOptions := options.Client().ApplyURI(mongoURI)
// mngoClient, err = mongo.NewClient(options.Client().ApplyURI(mongoURI))
// if err = mngoClient.Connect(MngoCtx); err != nil {
// logs.Critical("Mongodb NewClient %v: %v", mongoURI, err)
// MongoURL, err := beego.AppConfig.String(baseConfig + "::url")
// if err != nil {
// logs.Critical("MongoDB URI error: %v", err)
// panic(err)
// }
logs.Info("Connecting to %v", MongoURL)
clientOptions := options.Client().ApplyURI(MongoURL)
mngoClient, _ = mongo.NewClient(options.Client().ApplyURI(MongoURL))
if err = mngoClient.Connect(MngoCtx); err != nil {
logs.Critical("Mongodb NewClient %v: %v", MongoURL, err)
panic(err)
}
MngoCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Ping the primary
if mngoClient, err = mongo.Connect(MngoCtx, clientOptions); err != nil {
logs.Critical("Mongodb Connect %v: %v", mongoURI, err)
logs.Critical("Mongodb Connect %v: %v", MongoURL, err)
panic(err)
}
if err = mngoClient.Ping(MngoCtx, nil); err != nil {
logs.Critical("Mongodb Ping %v: %v", mongoURI, err)
logs.Critical("Mongodb Ping %v: %v", MongoURL, err)
panic(err)
}
logs.Info("Connecting mongo client to db %v", DBname)
mngoDB = mngoClient.Database(DBname)
MngoCollData = mngoDB.Collection(MngoNamesCollection.DATA)