Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Controller Methods for making DockerImage and Downloading Files #25

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/Neura-Launch-Dashboard.iml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

69 changes: 68 additions & 1 deletion builder/controllers/downloadFilesController.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
package controllers

import "github.com/gin-gonic/gin"
import (
"io"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/gin-gonic/gin"
)

type AWSConfig struct {
AWSAccessKeyID string
AWSSecretAccessKey string
AWSS3Region string
AWSS3Bucket string
AWSS3Client *s3.S3
}

func (config AWSConfig) ConnectToAWS() {
// Creating a new session
sess, err := session.NewSession(
&aws.Config{
Region: aws.String(config.AWSS3Region),
Credentials: credentials.NewStaticCredentials(config.AWSAccessKeyID, config.AWSSecretAccessKey, ""),
},
)

if err != nil {
return
}

config.AWSS3Client = s3.New(sess)
}

func (config AWSConfig) DownloadFilesFromS3() error {
// Defining Global S3 Client
s3Client := config.AWSS3Client

// Getting the object to download
downloader := s3.GetObjectInput{
Bucket: aws.String(config.AWSS3Bucket),
Key: aws.String("test.txt"),
}
Comment on lines +44 to +46
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im curious abt how this works... why "test.txt" ?


// Downloading the object
result, err := s3Client.GetObject(&downloader)

if err != nil {
return err
}

// Creating a Zip file
zipFile, err := os.Create("test.zip")
if err != nil {
return err
}

// Copying the object to the zip file
_, err = io.Copy(zipFile, result.Body)
if err != nil {
return err
}

return nil

}


func downloadFilesController(context *gin.Context) {
Copy link
Collaborator

@TarunTomar122 TarunTomar122 Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have the downloadFilesController ?


Expand Down
56 changes: 56 additions & 0 deletions builder/controllers/makeImageController.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package controllers

import (
"context"
"fmt"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/gin-gonic/gin"
)

func MakeDockerImage(ctx *gin.Context) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
ctx.String(500, "Error creating Docker Client")
return
}

// Build an image from a Dockerfile in the current directory
buildcontext, err := os.Open("./model.tar")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't the files zip?

Copy link
Collaborator Author

@kunxl-gg kunxl-gg Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to unzip and add the Dockerfile inside. I just made a tar after adding the Dockerfile.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are we adding the docker image and getting the tar file?

if err != nil {
ctx.String(500, "Error in reading file ", err)
return
}

// Building an image from the Dockerfile
buildResponse, err := cli.ImageBuild(
context.Background(),
buildcontext,
types.ImageBuildOptions{
Dockerfile: "model/Dockerfile",
Tags: []string{"my-image:latest"},
},
)
if err != nil {
ctx.String(500, "Error in Making Image %v", err)
return
}

// Reading the output of the build

// Making a buffer slice of size 1KB
buffer := make([]byte, 1024)

// Iterating through the build Response body
for {
n, err := buildResponse.Body.Read(buffer)
if n > 0 {
fmt.Print(string(buffer[:n]))
}
if err != nil {
ctx.String(500, "There was an error in building Image")
break
}
}
}
7 changes: 2 additions & 5 deletions builder/controllers/pingController.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import (
)

func PingController(context *gin.Context) {
context.JSON(
http.StatusOK,
gin.H{
"message": "Builder Service",
})
context.Header("Content-Type", "text/html")
context.String(http.StatusOK, "<h1>Hello World! from Builder Service</h1>")
}
18 changes: 17 additions & 1 deletion builder/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,48 @@ module github.com/LainForge/Neura-Launch-Dashboard/builder

go 1.21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When did this happen

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤷


require github.com/gin-gonic/gin v1.9.1
require (
github.com/aws/aws-sdk-go v1.45.28
github.com/gin-gonic/gin v1.9.1
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading