added GPU compatability

This commit is contained in:
2021-04-10 00:46:17 +04:00
parent bc1859484a
commit 2ec90cfaf5
11 changed files with 90 additions and 54 deletions

View File

@@ -2,6 +2,7 @@ package docker
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
@@ -11,8 +12,10 @@ import (
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"github.com/docker/go-connections/nat"
"github.com/lithammer/shortuuid"
"github.com/phayes/freeport"
"io"
"os/exec"
"time"
)
@@ -26,6 +29,7 @@ type DockerVM struct {
TagName string `json:"TagName"`
ImagePath string `json:"ImagePath"`
Ports []int `json:"OpenPorts"`
GPU string `json:"GPU"`
}
type ErrorLine struct {
@@ -39,7 +43,7 @@ type ErrorDetail struct {
var dockerRegistryUserID = ""
func BuildRunContainer(NumPorts int) (*DockerVM,error) {
func BuildRunContainer(NumPorts int, GPU string) (*DockerVM,error) {
//Docker Struct Variable
var RespDocker *DockerVM = new(DockerVM)
@@ -57,6 +61,9 @@ func BuildRunContainer(NumPorts int) (*DockerVM,error) {
RespDocker.Ports = append(RespDocker.Ports, Ports[i])
}
// Sets if GPU is selected or not
RespDocker.GPU =GPU
// Sets Free port to Struct
RespDocker.SSHPort = Ports[0]
RespDocker.VNCPort = Ports[1]
@@ -126,8 +133,9 @@ func (d *DockerVM)imageBuild(dockerClient *client.Client) error {
// Starts container and assigns port numbers
// Sample Docker run Command
// docker run -d=true --name=Test123 --restart=always -p 3443:6901 -p 3453:22
//-p 3434:3434 -p 3245:3245 -v=/opt/data:/data p2p-ubuntu /start > /dev/null
// docker run -d=true --name=Test123 --restart=always --gpus all
//-p 3443:6901 -p 3453:22 -p 3434:3434 -p 3245:3245 -v=/opt/data:/data
//p2p-ubuntu /start > /dev/null
func (d *DockerVM)runContainer(dockerClient *client.Client) error{
ctx, _ := context.WithTimeout(context.Background(), time.Second*2000)
@@ -173,33 +181,53 @@ func (d *DockerVM)runContainer(dockerClient *client.Client) error{
}
}
// The first mode runs using the Docker Api. As the API supports using
// CPU and uses a shell script for GPU call because till this point of
// implementation docker api does not support the flag "--gpu all"
if d.GPU != "true" {
config := &container.Config{
Image: d.TagName,
Entrypoint: []string{"/dockerstartup/vnc_startup.sh", "/start"},
Volumes: map[string]struct{}{"/opt/data:/data": {}},
ExposedPorts: ExposedPort,
}
hostConfig := &container.HostConfig{
PortBindings: PortForwarding,
}
config := &container.Config{
Image : d.TagName,
Entrypoint: [] string {"/dockerstartup/vnc_startup.sh","/start"},
Volumes: map[string]struct{}{"/opt/data:/data":{}},
ExposedPorts: ExposedPort,
res, err := dockerClient.ContainerCreate(ctx, config, hostConfig,
nil, nil, "")
// Set response ID
d.ID = res.ID
if err != nil {
return err
}
err = dockerClient.ContainerStart(ctx, res.ID, types.ContainerStartOptions{})
if err != nil {
return err
}
} else {
// Generate Random ID
id := shortuuid.New()
d.ID = id
var cmd bytes.Buffer
cmd.WriteString("docker run -d=true --name="+ id +" --restart=always --gpus all -p " + fmt.Sprint(d.VNCPort) + ":" + "6901 " + "-p " + fmt.Sprint(d.SSHPort) + ":" + "22 ")
for i := range d.Ports {
cmd.WriteString("-p " + fmt.Sprint(d.Ports[i]) + ":" + fmt.Sprint(d.Ports[i]) + " ")
}
cmd.WriteString("-v=/opt/data:/data p2p-ubuntu /start > /dev/null")
//"-v=/opt/data:/data p2p-ubuntu /start > /dev/null"
cmdStr := cmd.String()
_, err := exec.Command("/bin/sh", "-c", cmdStr).Output()
if err != nil {
return err
}
}
hostConfig := &container.HostConfig{
PortBindings: PortForwarding,
}
res, err := dockerClient.ContainerCreate(ctx,config,hostConfig,
nil,nil,"")
// Set response ID
d.ID = res.ID
if err != nil {
return err
}
err = dockerClient.ContainerStart(ctx, res.ID, types.ContainerStartOptions{})
if err != nil {
return err
}
return nil
}

View File

@@ -20,7 +20,7 @@ type Docker struct {
// Starts container using RPC calls
func (l *Listener) StartContainer( reply *Docker) error {
fmt.Print("here")
vm, err := docker.BuildRunContainer(3)
vm, err := docker.BuildRunContainer(3,"false")
if err != nil {
return err
}

View File

@@ -36,9 +36,13 @@ func Server() error{
//Gets Ip Table from server node
r.POST("/IpTable", func(c *gin.Context) {
// Getting IPV4 address of client
var ClientHost p2p.IpAddress
ClientHost.Ipv4 = c.ClientIP()
// Variable to store IP table information
var IPTable p2p.IpAddresses
//Add Client IP address to IPTable struct
IPTable.IpAddress = append(IPTable.IpAddress, ClientHost)
// Receive file from POST request
body, err := c.FormFile("json")
@@ -77,8 +81,9 @@ func Server() error{
// Starts docker container in server
r.GET("/startcontainer", func(c *gin.Context) {
// Get Number of ports to open
// Get Number of ports to open and whether to use GPU or not
Ports := c.DefaultQuery("ports","0")
GPU := c.DefaultQuery("GPU","false")
var PortsInt int
// Convert Get Request value to int
@@ -86,7 +91,7 @@ func Server() error{
// Creates container and returns back result to
// access container
resp, err := docker.BuildRunContainer(PortsInt)
resp, err := docker.BuildRunContainer(PortsInt,GPU)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))