From 221bd10eb746f80acaa05a61d74fe8a5fc655945 Mon Sep 17 00:00:00 2001 From: Akilan Date: Mon, 8 Feb 2021 01:43:28 +0400 Subject: [PATCH] added docker container go SDK and build container implemented --- Makefile | 3 +- docs/DesignVIrtualization | 11 ++++ go.mod | 2 +- server/docker/docker.go | 120 +++++++++++++++++++++++++++++++++++++- server/server.go | 18 +++++- 5 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 docs/DesignVIrtualization diff --git a/Makefile b/Makefile index 6714eae..8263bd3 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/docs/DesignVIrtualization b/docs/DesignVIrtualization new file mode 100644 index 0000000..26a2425 --- /dev/null +++ b/docs/DesignVIrtualization @@ -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. \ No newline at end of file diff --git a/go.mod b/go.mod index 8f6702e..436812b 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/server/docker/docker.go b/server/docker/docker.go index 92a37cb..97e954b 100644 --- a/server/docker/docker.go +++ b/server/docker/docker.go @@ -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" -} \ No newline at end of file + +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 +} diff --git a/server/server.go b/server/server.go index d840bb5..0d4314f 100644 --- a/server/server.go +++ b/server/server.go @@ -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