Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Jul 10, 2021
1 parent 6035c9f commit 2e45431
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM golang:1.16.5

ARG PEANUT_VERSION=0.1.6
ARG PEANUT_VERSION=0.1.7

ENV GO111MODULE=on

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<img src="https://github.com/Clivern/Peanut/workflows/Release/badge.svg">
</a>
<a href="https://github.com/Clivern/Peanut/releases">
<img src="https://img.shields.io/badge/Version-0.1.6-red.svg">
<img src="https://img.shields.io/badge/Version-0.1.7-red.svg">
</a>
<a href="https://goreportcard.com/report/github.com/Clivern/Peanut">
<img src="https://goreportcard.com/badge/github.com/Clivern/Peanut?v=0.1.6">
<img src="https://goreportcard.com/badge/github.com/Clivern/Peanut?v=0.1.7">
</a>
<a href="https://godoc.org/github.com/clivern/peanut">
<img src="https://godoc.org/github.com/clivern/peanut?status.svg">
Expand Down
75 changes: 75 additions & 0 deletions core/controller/misc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2021 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package controller

// Message interface
type Message interface {
GetCorrelation() string
GetService() string
GetJob() string
GetType() string
}

// DeployRequest type
type DeployRequest struct {
JobID string `json:"jobId"`
ServiceID string `json:"serviceId"`
Template string `json:"template"`
Configs map[string]string `json:"configs"`
DeleteAfter string `json:"deleteAfter"`
Type string `json:"type"`
CorrelationID string `json:"correlationID"`
}

// DestroyRequest type
type DestroyRequest struct {
JobID string `json:"jobId"`
ServiceID string `json:"serviceId"`
Template string `json:"template"`
Configs map[string]string `json:"configs"`
DeleteAfter string `json:"deleteAfter"`
Type string `json:"type"`
CorrelationID string `json:"correlationID"`
}

// GetCorrelation gets the correlation id
func (d DeployRequest) GetCorrelation() string {
return d.CorrelationID
}

// GetService gets the service id
func (d DeployRequest) GetService() string {
return d.ServiceID
}

// GetJob gets the job id
func (d DeployRequest) GetJob() string {
return d.JobID
}

// GetType gets the job type
func (d DeployRequest) GetType() string {
return d.Type
}

// GetCorrelation gets the correlation id
func (d DestroyRequest) GetCorrelation() string {
return d.CorrelationID
}

// GetService gets the service id
func (d DestroyRequest) GetService() string {
return d.ServiceID
}

// GetJob gets the job id
func (d DestroyRequest) GetJob() string {
return d.JobID
}

// GetType gets the job type
func (d DestroyRequest) GetType() string {
return d.Type
}
100 changes: 29 additions & 71 deletions core/controller/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,6 @@ import (
"github.com/spf13/viper"
)

// Message interface
type Message interface {
GetCorrelation() string
GetService() string
GetJob() string
GetType() string
}

// DeployRequest type
type DeployRequest struct {
JobID string `json:"jobId"`
ServiceID string `json:"serviceId"`
Template string `json:"template"`
Configs map[string]string `json:"configs"`
DeleteAfter string `json:"deleteAfter"`
Type string `json:"type"`
CorrelationID string `json:"correlationID"`
}

// DestroyRequest type
type DestroyRequest struct {
JobID string `json:"jobId"`
ServiceID string `json:"serviceId"`
Type string `json:"type"`
CorrelationID string `json:"correlationID"`
}

// GetCorrelation gets the correlation id
func (d DeployRequest) GetCorrelation() string {
return d.CorrelationID
}

// GetService gets the service id
func (d DeployRequest) GetService() string {
return d.ServiceID
}

// GetJob gets the job id
func (d DeployRequest) GetJob() string {
return d.JobID
}

// GetType gets the job type
func (d DeployRequest) GetType() string {
return d.Type
}

// GetCorrelation gets the correlation id
func (d DestroyRequest) GetCorrelation() string {
return d.CorrelationID
}

// GetService gets the service id
func (d DestroyRequest) GetService() string {
return d.ServiceID
}

// GetJob gets the job id
func (d DestroyRequest) GetJob() string {
return d.JobID
}

// GetType gets the job type
func (d DestroyRequest) GetType() string {
return d.Type
}

// Workers type
type Workers struct {
job *model.Job
Expand Down Expand Up @@ -128,7 +61,7 @@ func (w *Workers) DeployRequest(c *gin.Context, rawBody []byte) {
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error(`Invalid message`)
}).Debug(`Invalid message`)

c.JSON(http.StatusBadRequest, gin.H{
"correlationID": c.GetHeader("x-correlation-id"),
Expand Down Expand Up @@ -192,13 +125,27 @@ func (w *Workers) DestroyRequest(c *gin.Context, rawBody []byte) {
Type: "DestroyRequest",
}

service, err := w.service.GetRecord(c.Param("serviceId"))

if err != nil {
c.JSON(http.StatusNotFound, gin.H{
"correlationID": c.GetHeader("x-correlation-id"),
"errorMessage": "Error! service not found",
})
return
}

message.Template = service.Template
message.Configs = service.Configs
message.DeleteAfter = service.DeleteAfter

log.WithFields(log.Fields{
"correlation_id": message.GetCorrelation(),
"message": message,
}).Info(`Incoming request`)

// Create a async job
err := w.job.CreateRecord(model.JobRecord{
err = w.job.CreateRecord(model.JobRecord{
ID: message.JobID,
Action: model.DestroyJob,
Service: model.ServiceRecord{
Expand Down Expand Up @@ -263,16 +210,27 @@ func (w *Workers) ProcessRequest(notifyChannel chan<- Message, wg *sync.WaitGrou
switch message.GetType() {
case "DeployRequest":
// Deploy the service
err = w.containerization.Deploy(model.ServiceRecord{
depr := DeployRequest{}
result := make(map[string]string)

result, err = w.containerization.Deploy(model.ServiceRecord{
ID: message.(DeployRequest).ServiceID,
Template: message.(DeployRequest).Template,
Configs: message.(DeployRequest).Configs,
DeleteAfter: message.(DeployRequest).DeleteAfter,
})

// Override configs
depr = message.(DeployRequest)
depr.Configs = result
message = depr
case "DestroyRequest":
// Destroy the service
err = w.containerization.Destroy(model.ServiceRecord{
ID: message.(DestroyRequest).ServiceID,
ID: message.(DestroyRequest).ServiceID,
Template: message.(DestroyRequest).Template,
Configs: message.(DestroyRequest).Configs,
DeleteAfter: message.(DestroyRequest).DeleteAfter,
})
default:
log.WithFields(log.Fields{
Expand Down
4 changes: 3 additions & 1 deletion core/definition/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

const (
// RedisPort const
RedisPort = "6379"
RedisPort = "6379"
RedisPortStart = 6300
RedisPortEnd = 6400
)

// GetRedisConfig gets yaml definition object
Expand Down
1 change: 0 additions & 1 deletion core/model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type Service struct {

// GetConfig get a config value
func (s *ServiceRecord) GetConfig(key, def string) string {

if val, ok := s.Configs[key]; ok {
return val
}
Expand Down
2 changes: 1 addition & 1 deletion core/runtime/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ const (

// Containerization interface
type Containerization interface {
Deploy(service model.ServiceRecord) error
Deploy(service model.ServiceRecord) (map[string]string, error)
Destroy(service model.ServiceRecord) error
}
95 changes: 90 additions & 5 deletions core/runtime/docker_compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package runtime

import (
"fmt"
"strconv"

"github.com/clivern/peanut/core/definition"
"github.com/clivern/peanut/core/model"
Expand All @@ -26,25 +27,109 @@ func NewDockerCompose() *DockerCompose {
}

// Deploy deploys services
func (d *DockerCompose) Deploy(service model.ServiceRecord) error {
func (d *DockerCompose) Deploy(service model.ServiceRecord) (map[string]string, error) {
data := make(map[string]string)

// Deploy redis service
if model.RedisService == service.Template {
return d.deployRedis(service)
}

return nil
return data, fmt.Errorf("Error! Undefined service")
}

// Destroy destroys services
func (d *DockerCompose) Destroy(service model.ServiceRecord) error {
if model.RedisService == service.Template {
return d.destroyRedis(service)
}

return nil
}

// deployRedis deploys a redis
func (d *DockerCompose) deployRedis(service model.ServiceRecord) error {
func (d *DockerCompose) deployRedis(service model.ServiceRecord) (map[string]string, error) {
data := make(map[string]string)

if service.Configs != nil {
data = service.Configs
}

data["address"] = "[NodeIp]"
data["port"] = service.GetConfig("port", strconv.Itoa(util.Rand(
definition.RedisPortStart,
definition.RedisPortEnd,
)))

data["password"] = service.GetConfig("password", "")

redis := definition.GetRedisConfig(
service.GetConfig("image", RedisDockerImage),
service.GetConfig("port", definition.RedisPort),
data["port"],
service.GetConfig("restartPolicy", "unless-stopped"),
data["password"],
)

result, err := redis.ToString()

if err != nil {
return data, err
}

err = util.StoreFile(
fmt.Sprintf("%s/%s.yml", viper.GetString("app.storage.path"), service.ID),
result,
)

if err != nil {
return data, err
}

command := fmt.Sprintf(
"docker-compose -f %s/%s.yml up -d --force-recreate",
viper.GetString("app.storage.path"),
service.ID,
)

stdout, stderr, err := util.Exec(command)

log.WithFields(log.Fields{
"command": command,
}).Info("Run a shell command")

if err != nil {
return data, err
}

// Store runtime verbose logs only in dev environment
if viper.GetString("app.mode") == "dev" {
err = util.StoreFile(
fmt.Sprintf("%s/%s.stdout.log", viper.GetString("app.storage.path"), service.ID),
stdout,
)

if err != nil {
return data, err
}

err = util.StoreFile(
fmt.Sprintf("%s/%s.stderr.log", viper.GetString("app.storage.path"), service.ID),
stderr,
)

if err != nil {
return data, err
}
}

return data, nil
}

// destroyRedis destroys redis
func (d *DockerCompose) destroyRedis(service model.ServiceRecord) error {
redis := definition.GetRedisConfig(
service.GetConfig("image", RedisDockerImage),
service.GetConfig("port", ""),
service.GetConfig("restartPolicy", "unless-stopped"),
service.GetConfig("password", ""),
)
Expand All @@ -65,7 +150,7 @@ func (d *DockerCompose) deployRedis(service model.ServiceRecord) error {
}

command := fmt.Sprintf(
"docker-compose -f %s/%s.yml up -d --force-recreate",
"docker-compose -f %s/%s.yml down --rmi all --remove-orphans",
viper.GetString("app.storage.path"),
service.ID,
)
Expand Down
Loading

0 comments on commit 2e45431

Please sign in to comment.