added docker container go SDK and build container implemented

This commit is contained in:
2021-02-08 01:43:28 +04:00
parent a2adff2318
commit 221bd10eb7
5 changed files with 147 additions and 7 deletions

View File

@@ -1,7 +1,8 @@
build:
go build -o bin/main main.go
ADMIN_USER=admin ADMIN_PASSWORD=admin docker-compose -f server/docker/dockprom/docker-compose.yml up -d
run:
go run main.go
dockerproc:
ADMIN_USER=admin ADMIN_PASSWORD=admin docker-compose -f server/docker/dockprom/docker-compose.yml up -d

11
docs/DesignVIrtualization Normal file
View File

@@ -0,0 +1,11 @@
Virtualization Design
======================
In this Repo the initial choice is to use docker containers and to use it's default parameters when creating
a container. Alphine linux will be the Default OS used because of being light weight.
Methods to be created
- Build OS Image from DockerFile
- Run Image Built
- Possibility to kill image by server admin or client side user.
- Track stats of the docker container by server admin or client side user.

2
go.mod
View File

@@ -8,7 +8,7 @@ require (
github.com/christophwitzko/go-curl v0.0.0-20171216141518-4203158d6acb
github.com/containerd/containerd v1.4.3 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.2+incompatible // indirect
github.com/docker/docker v20.10.2+incompatible
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fsouza/go-dockerclient v1.7.0 // indirect

View File

@@ -1,8 +1,122 @@
package docker
import (
//log"
"io/ioutil"
"io"
"context"
"bytes"
"os"
"archive/tar"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
)
func BuildContainer() string{
return "test"
}
type SSHreturn struct {
Port string `bson:port`
Username string `bson:username`
Password string `bson:password`
}
// Runs a docker contianer with default settings
// TODO implement with public keys
func RunVM()(interface{},error) {
client, err := client.NewEnvClient()
if err != nil {
//log.Fatalf("Unable to create docker client: %s", err)
return nil,err
}
// Client, imagename and Dockerfile location
tags := []string{"p2p-ubuntu"}
dockerfile := "./server/docker/containers/docker-ubuntu-sshd/Dockerfile"
err = BuildImage(client, tags, dockerfile)
if err != nil {
//log.Println(err)
return nil,err
}
// TODO Run contianer
sshreturn := new(SSHreturn)
sshreturn.Port = "1003"
sshreturn.Password = "password"
sshreturn.Username = "master"
return sshreturn, nil
}
// Taken from (https://medium.com/@Frikkylikeme/controlling-docker-with-golang-code-b213d9699998)
func BuildImage(client *client.Client, tags []string, dockerfile string) error{
ctx := context.Background()
// Create a buffer
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
// Create a filereader
dockerFileReader, err := os.Open(dockerfile)
if err != nil {
return err
}
// Read the actual Dockerfile
readDockerFile, err := ioutil.ReadAll(dockerFileReader)
if err != nil {
return err
}
// Make a TAR header for the file
tarHeader := &tar.Header{
Name: dockerfile,
Size: int64(len(readDockerFile)),
}
// Writes the header described for the TAR file
err = tw.WriteHeader(tarHeader)
if err != nil {
return err
}
// Writes the dockerfile data to the TAR file
_, err = tw.Write(readDockerFile)
if err != nil {
return err
}
dockerFileTarReader := bytes.NewReader(buf.Bytes())
// Define the build options to use for the file
// https://godoc.org/github.com/docker/docker/api/types#ImageBuildOptions
buildOptions := types.ImageBuildOptions{
Context: dockerFileTarReader,
Dockerfile: dockerfile,
Remove: true,
Tags: tags,
}
// Build the actual image
imageBuildResponse, err := client.ImageBuild(
ctx,
dockerFileTarReader,
buildOptions,
)
if err != nil {
return err
}
// Read the STDOUT from the build process
defer imageBuildResponse.Body.Close()
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
return err
}
return nil
}

View File

@@ -3,6 +3,7 @@ package server
import (
"github.com/gin-gonic/gin"
"net/http"
"fmt"
docker "git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
)
@@ -14,8 +15,21 @@ func Server() {
c.JSON(http.StatusOK, ServerInfo())
})
r.GET("/create_vm", func(c *gin.Context) {
c.JSON(http.StatusOK, docker.BuildContainer())
r.GET("/create_vm/:virtualization", func(c *gin.Context) {
virtualization := c.Param("virtualization")
// Runs based on Preallocated VM size
if virtualization == "docker" {
sshinfo,err := docker.RunVM()
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
}
if sshinfo != nil {
c.JSON(http.StatusOK, sshinfo)
}
} else {
c.String(200,"virtualization tool not selected")
}
})
// Port running on