Skip to content

Commit

Permalink
feat: injection variables #1
Browse files Browse the repository at this point in the history
  • Loading branch information
coding-convention committed Jul 30, 2023
1 parent a3bbf3c commit bb3d7c3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
32 changes: 29 additions & 3 deletions api/v1.0/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
conf "github.com/sw-maestro-kumofactory/miz-ball/config"

"github.com/sw-maestro-kumofactory/miz-ball/utils/dockerclient"
"github.com/sw-maestro-kumofactory/miz-ball/utils/dockerfilegenerator"
"github.com/sw-maestro-kumofactory/miz-ball/utils/ecr"
rep "github.com/sw-maestro-kumofactory/miz-ball/utils/repomanagement"

dockerfilesamples "github.com/sw-maestro-kumofactory/miz-ball/utils/dockerfilegenerator/dockerfile-samples"
samplebuilder "github.com/sw-maestro-kumofactory/miz-ball/utils/dockerfilegenerator/sample-builder"
)

// TODO: add validation
Expand Down Expand Up @@ -48,6 +49,7 @@ type DeployInfo struct {
Language string `json:"Language"`
Runtime string `json:"Runtime"`
Compiler string `json:"Compiler"`
Env []EnvInfo `json:"env"`
}

type PortBindInfo struct {
Expand All @@ -56,6 +58,11 @@ type PortBindInfo struct {
Bind []string `json:"bind"`
}

type EnvInfo struct {
Key string `json:"key"`
Value string `json:"value"`
}

var rootDir = "/app/repository/"

func ApplicationDeploy2(c *gin.Context) {
Expand Down Expand Up @@ -100,14 +107,33 @@ func ApplicationDeploy2(c *gin.Context) {
srcDir := filepath.Join(repoDir, folderName)
dstDir := repoDir

// TODO: wrap this code
builder := dockerfilegenerator.NewBuilder()
if info.Env != nil {
for _, env := range info.Env {
builder.AddEnv(env.Key, env.Value)
}
}
if !info.Dockerfile {
if info.Language == "node" {
dockerfilesamples.NodeApplication(srcDir)
samplebuilder.AddNodeBuilder(builder)
// samplebuilder.NodeApplication(srcDir)

} else if info.Language == "java" {
dockerfilesamples.JavaApplication(srcDir)
samplebuilder.AddJavaBuilder(builder)
// samplebuilder.JavaApplication(srcDir)
}
} else if info.Dockerfile {
dockerfilePath := filepath.Join(srcDir, "Dockerfile")
dockerfileStream, err := os.ReadFile(dockerfilePath)
if err != nil {
fmt.Println("error")
}
builder.AddDockerfile(dockerfileStream)
os.Remove(dockerfilePath)
}
builder.CreateDockerfile(srcDir, "Dockerfile")
// until here

err = rep.CompressToTarGz(srcDir, dstDir)
if handleError(c, err, http.StatusBadRequest) {
Expand Down
16 changes: 14 additions & 2 deletions utils/dockerfilegenerator/dockerfilegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dockerfilegenerator
import (
"bytes"
"fmt"
"io/ioutil"
"os"
)

type Builder struct {
Expand All @@ -18,18 +18,30 @@ func (b *Builder) AddDirective(instruction, arguments string) {
b.buffer.WriteString(fmt.Sprintf("%s %s\n", instruction, arguments))
}

func (b *Builder) AddCommand(command string) {
b.buffer.WriteString(fmt.Sprintf("%s\n", command))
}

func (b *Builder) AddEnv(key, value string) {
b.buffer.WriteString(fmt.Sprintf("ENV %s=%s\n", key, value))
}

func (b *Builder) Bytes() ([]byte, error) {
return b.buffer.Bytes(), nil
}

func (b *Builder) AddDockerfile(dockerfile []byte) {
b.buffer.Write(dockerfile)
}

func (b *Builder) CreateDockerfile(directory, filename string) error {
content, err := b.Bytes()
if err != nil {
return err
}

path := fmt.Sprintf("%s/%s", directory, filename)
err = ioutil.WriteFile(path, content, 0644)
err = os.WriteFile(path, content, 0644)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dockerfilesamples
package samplebuilder

import (
"github.com/sw-maestro-kumofactory/miz-ball/utils/dockerfilegenerator"
Expand Down Expand Up @@ -27,3 +27,19 @@ func JavaApplication(targetDir string) {
panic(err)
}
}

func AddJavaBuilder(builder *dockerfilegenerator.Builder) {
// Build the first stage
builder.AddDirective(dfenum.FROM, "gradle:jdk17-alpine AS builder")
builder.AddDirective(dfenum.WORKDIR, "/src")
builder.AddDirective(dfenum.COPY, ". .")
builder.AddDirective(dfenum.RUN, "ls")
builder.AddDirective(dfenum.RUN, "gradle build && cd build/libs && ls && rm $(ls *plain.jar) && mv $(ls *.jar) app.jar")

// Build the second stage
builder.AddDirective(dfenum.FROM, "openjdk:17-alpine")
builder.AddDirective(dfenum.WORKDIR, "/app")
builder.AddDirective(dfenum.COPY, "--from=builder /src/build/libs /app")
builder.AddDirective(dfenum.EXPOSE, "8080")
builder.AddDirective(dfenum.ENTRYPOINT, `["java","-jar","./app.jar"]`)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dockerfilesamples
package samplebuilder

import (
"github.com/sw-maestro-kumofactory/miz-ball/utils/dockerfilegenerator"
Expand Down Expand Up @@ -26,3 +26,18 @@ func NodeApplication(targetDir string) {
panic(err)
}
}

func AddNodeBuilder(builder *dockerfilegenerator.Builder) {
builder.AddDirective(dfenum.FROM, "node:16-alpine AS builder")
builder.AddDirective(dfenum.WORKDIR, "/app")
builder.AddDirective(dfenum.COPY, ". .")
builder.AddDirective(dfenum.RUN, "npm install")
builder.AddDirective(dfenum.RUN, "npm run build")

builder.AddDirective(dfenum.FROM, "node:16-alpine")
builder.AddDirective(dfenum.WORKDIR, "/app")
builder.AddDirective(dfenum.COPY, "--from=builder /app /app")

builder.AddDirective(dfenum.EXPOSE, "3000")
builder.AddDirective(dfenum.ENTRYPOINT, `["npm","run","start"]`)
}

0 comments on commit bb3d7c3

Please sign in to comment.