Skip to content

Commit

Permalink
Merge pull request #91 from Clivern/feature/deployment
Browse files Browse the repository at this point in the history
Feature/deployment
  • Loading branch information
Clivern authored Apr 27, 2020
2 parents 8b79803 + b141fde commit 37f4935
Show file tree
Hide file tree
Showing 28 changed files with 725 additions and 307 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

config.prod.yml
config.prod.yml

# dist dir
dist
41 changes: 21 additions & 20 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
---
archives:
-
replacements:
386: i386
amd64: x86_64
darwin: Darwin
linux: Linux
windows: Windows
before:
hooks:
# you may remove this if you don't use vgo
- go mod download
# you may remove this if you don't need go generate
- go generate ./...
- "go mod download"
- "go generate ./..."
builds:
- env:
- CGO_ENABLED=0
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
-
env:
- CGO_ENABLED=0
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- "^docs:"
- "^test:"
sort: asc
checksum:
name_template: checksums.txt
snapshot:
name_template: "{{ .Tag }}-next"
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ EXPOSE 8080
VOLUME /app/configs
VOLUME /app/var

HEALTHCHECK --interval=5s --timeout=2s --retries=5 --start-period=2s \
CMD ./beetle --config /app/configs/config.dist.yml --get health
./beetle version

CMD ["./beetle", "--config", "/app/configs/config.dist.yml"]
CMD ["./beetle", "serve", "-c", "/app/configs/config.dist.yml"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ ci: style check_license test vet lint
## run: Run the service
run:
-cp -n config.dist.yml config.prod.yml
$(GO) run beetle.go
$(GO) run beetle.go serve -c config.prod.yml


## doc: Serve the docs.
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $ curl -sL https://github.com/Clivern/Beetle/releases/download/x.x.x/beetle_x.x.
Create your config file as explained on [development part](#development) and run beetle with systemd or anything else you prefer.

```
$ ./beetle -config=/custom/path/config.prod.yml
$ ./beetle serve -c /custom/path/config.prod.yml
```


Expand Down Expand Up @@ -119,15 +119,15 @@ And then run the application.
```zsh
$ go build beetle.go
$ ./beetle
$ ./beetle serve -c /custom/path/config.prod.yml

// OR

$ make run

// To Provide a custom config file
$ ./beetle -config=/custom/path/config.prod.yml
$ go run beetle.go -config=/custom/path/config.prod.yml
// OR

$ go run beetle.go serve -c /custom/path/config.prod.yml
```

## Versioning
Expand Down
201 changes: 7 additions & 194 deletions beetle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,209 +5,22 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"

"github.com/clivern/beetle/internal/app/cmd"
"github.com/clivern/beetle/internal/app/controller"
"github.com/clivern/beetle/internal/app/middleware"
"github.com/clivern/beetle/internal/app/module"

"github.com/drone/envsubst"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)

func main() {
var configFile string
var get string

flag.StringVar(&configFile, "config", "config.prod.yml", "Absolute path to config file")
flag.StringVar(&get, "get", "", "Use with version or release (./beetle --get release) (./beetle --get version) (./beetle --get health)")
flag.Parse()

if get == "release" || get == "version" {
fmt.Println(
fmt.Sprintf(
`Beetle Version %v Commit %v, Built @%v`,
version,
commit,
date,
),
)
return
}

configUnparsed, err := ioutil.ReadFile(configFile)

if err != nil {
panic(fmt.Sprintf(
"Error while reading config file [%s]: %s",
configFile,
err.Error(),
))
}

configParsed, err := envsubst.EvalEnv(string(configUnparsed))

if err != nil {
panic(fmt.Sprintf(
"Error while parsing config file [%s]: %s",
configFile,
err.Error(),
))
}

viper.SetConfigType("yaml")
err = viper.ReadConfig(bytes.NewBuffer([]byte(configParsed)))

if err != nil {
panic(fmt.Sprintf(
"Error while loading configs [%s]: %s",
configFile,
err.Error(),
))
}

if viper.GetString("log.output") != "stdout" {
fs := module.FileSystem{}
dir, _ := filepath.Split(viper.GetString("log.output"))

if !fs.DirExists(dir) {
if _, err := fs.EnsureDir(dir, 777); err != nil {
panic(fmt.Sprintf(
"Directory [%s] creation failed with error: %s",
dir,
err.Error(),
))
}
}

if !fs.FileExists(viper.GetString("log.output")) {
f, err := os.Create(viper.GetString("log.output"))
if err != nil {
panic(fmt.Sprintf(
"Error while creating log file [%s]: %s",
viper.GetString("log.output"),
err.Error(),
))
}
defer f.Close()
}
}

if get == "health" {
cmd.HealthCheck()
return
}

if get == "ready" {
cmd.ReadyCheck()
return
}

if viper.GetString("log.output") == "stdout" {
gin.DefaultWriter = os.Stdout
log.SetOutput(os.Stdout)
} else {
f, _ := os.Create(viper.GetString("log.output"))
gin.DefaultWriter = io.MultiWriter(f)
}

if viper.GetString("log.level") == "info" {
log.SetLevel(log.InfoLevel)
}

if viper.GetString("app.mode") == "prod" {
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = ioutil.Discard
gin.DisableConsoleColor()
}

log.SetFormatter(&log.JSONFormatter{})

// Init DB Connection
db := module.Database{}
err = db.AutoConnect()

if err != nil {
panic(err.Error())
}

// Migrate Database
success := db.Migrate()

if !success {
panic("Error! Unable to migrate database tables.")
}

defer db.Close()

messages := make(chan string, viper.GetInt("app.broker.native.capacity"))

r := gin.Default()

r.Use(middleware.Correlation())
r.Use(middleware.Logger())
r.Use(middleware.Metric())

r.GET("/favicon.ico", func(c *gin.Context) {
c.String(http.StatusNoContent, "")
})

r.GET("/", controller.HealthCheck)
r.GET("/_health", controller.HealthCheck)
r.GET("/_ready", controller.ReadyCheck)
r.GET(viper.GetString("app.metrics.prometheus.endpoint"), gin.WrapH(controller.Metrics()))
r.GET("/api/v1/cluster", controller.Clusters)
r.GET("/api/v1/cluster/:cn", controller.Cluster)
r.GET("/api/v1/cluster/:cn/namespace", controller.Namespaces)
r.GET("/api/v1/cluster/:cn/namespace/:ns", controller.Namespace)
r.GET("/api/v1/cluster/:cn/namespace/:ns/app", controller.Applications)
r.GET("/api/v1/cluster/:cn/namespace/:ns/app/:id", controller.Application)
r.POST("/api/v1/cluster/:cn/namespace/:ns/app/:id/deployment", func(c *gin.Context) {
controller.CreateDeployment(c, messages)
})
r.POST("/api/v1/cluster/:cn/namespace/:ns/app/:id/rollback", func(c *gin.Context) {
controller.CreateRollback(c, messages)
})
r.GET("/api/v1/job", controller.Jobs)
r.GET("/api/v1/job/:uuid", controller.GetJob)
r.DELETE("/api/v1/job/:uuid", controller.DeleteJob)

for i := 0; i < viper.GetInt("app.broker.native.workers"); i++ {
go controller.Worker(i+1, messages)
}

var runerr error

if viper.GetBool("app.tls.status") {
runerr = r.RunTLS(
fmt.Sprintf(":%s", strconv.Itoa(viper.GetInt("app.port"))),
viper.GetString("app.tls.pemPath"),
viper.GetString("app.tls.keyPath"),
)
} else {
runerr = r.Run(
fmt.Sprintf(":%s", strconv.Itoa(viper.GetInt("app.port"))),
)
}
// Expose build info to cmd subpackage to avoid custom ldflags
cmd.Version = version
cmd.Commit = commit
cmd.Date = date
cmd.BuiltBy = builtBy

if runerr != nil {
panic(runerr.Error())
}
cmd.Execute()
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ require (
github.com/drone/envsubst v1.0.2
github.com/gin-gonic/gin v1.6.2
github.com/jinzhu/gorm v1.9.12
github.com/olekukonko/tablewriter v0.0.4
github.com/prometheus/client_golang v1.5.1
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.5.0
github.com/spf13/cobra v1.0.0
github.com/spf13/viper v1.6.3
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
k8s.io/api v0.18.2
Expand Down
Loading

0 comments on commit 37f4935

Please sign in to comment.