added code to detect docker containers

This commit is contained in:
2021-06-09 16:43:59 +04:00
parent 4141f1c575
commit 8466e2d1ca
6 changed files with 84 additions and 12 deletions

View File

@@ -9,7 +9,8 @@ var (
defaultPath string
defaults = map[string]interface{}{
"IPTable": "/etc/p2p-rendering/ip_table.json",
"DockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/",
"DockerContainers": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/",
"DefaultDockerFile": "/home/akilan/Documents/p2prendering/p2p-redering-computation/server/docker/containers/docker-ubuntu-sshd/",
"SpeedTestFile":"/etc/p2p-rendering/50.bin",
}
configName = "config"
@@ -19,9 +20,10 @@ var (
)
type Config struct {
IPTable string
DockerFile string
SpeedTestFile string
IPTable string
DockerContainers string
DefaultDockerFile string
SpeedTestFile string
}
// Exists reports whether the named file or directory exists.
@@ -45,7 +47,8 @@ func SetDefaults() error {
//Setting default paths for the config file
defaults["IPTable"] = defaultPath + "p2p/ip_table.json"
defaults["DockerFile"] = defaultPath + "server/docker/containers/docker-ubuntu-sshd/"
defaults["DefaultDockerFile"] = defaultPath + "server/docker/containers/docker-ubuntu-sshd/"
defaults["DockerContainers"] = defaultPath + "server/docker/containers/"
defaults["SpeedTestFile"] = defaultPath + "p2p/50.bin"
//Paths to search for config file

View File

@@ -0,0 +1 @@
Simple Ubuntu 20.04 image

View File

@@ -16,6 +16,7 @@ import (
"github.com/lithammer/shortuuid"
"github.com/phayes/freeport"
"io"
"io/ioutil"
"os/exec"
"time"
)
@@ -33,6 +34,15 @@ type DockerVM struct {
GPU string `json:"GPU"`
}
type DockerContainers struct {
DockerContainer []DockerContainer `json:"DockerContainer"`
}
type DockerContainer struct {
ContainerName string `json:"DockerContainerName"`
ContainerDescription string `json:"ContainerDescription"`
}
type ErrorLine struct {
Error string `json:"error"`
ErrorDetail ErrorDetail `json:"errorDetail"`
@@ -44,7 +54,7 @@ type ErrorDetail struct {
var dockerRegistryUserID = ""
func BuildRunContainer(NumPorts int, GPU string) (*DockerVM,error) {
func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerVM,error) {
//Docker Struct Variable
var RespDocker *DockerVM = new(DockerVM)
@@ -81,7 +91,7 @@ func BuildRunContainer(NumPorts int, GPU string) (*DockerVM,error) {
if err != nil {
return nil,err
}
RespDocker.ImagePath = config.DockerFile
RespDocker.ImagePath = config.DefaultDockerFile
// Gets docker information from env variables
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
@@ -238,7 +248,7 @@ func (d *DockerVM)runContainer(dockerClient *client.Client) error{
return nil
}
// StopAndRemoveContainer TODO: Implement and remove docker instance running
// StopAndRemoveContainer
// Stop and remove a container
// Reference (https://gist.github.com/frikky/e2efcea6c733ea8d8d015b7fe8a91bf6)
func StopAndRemoveContainer(containername string) error {
@@ -266,6 +276,47 @@ func StopAndRemoveContainer(containername string) error {
return nil
}
// ViewAllContainers returns all containers runnable and which can be built
func ViewAllContainers() (*DockerContainers, error){
// Traverse the folder path as per given in the config file
config, err := config.ConfigInit()
if err != nil {
return nil,err
}
folders, err := ioutil.ReadDir(config.DockerContainers)
if err != nil {
return nil, err
}
//Declare variable DockerContainers of type struct
var Containers *DockerContainers = new(DockerContainers)
for _, f := range folders {
if f.IsDir() {
fmt.Print(f.Name())
//Declare variable DockerContainer of type struct
var Container DockerContainer
// Setting container name to folder name
Container.ContainerName = f.Name()
// Getting Description from file description.txt
Description, err := ioutil.ReadFile(config.DockerContainers + "/" + Container.ContainerName + "/description.txt")
// if we os.Open returns an error then handle it
if err != nil {
return nil, err
}
// Get Description from description.txt
Container.ContainerDescription = string(Description)
Containers.DockerContainer = append(Containers.DockerContainer, Container)
}
}
return Containers,nil
}
func print(rd io.Reader) error {
var lastLine string

View File

@@ -7,7 +7,7 @@ import (
func TestDocker(t *testing.T) {
//TODO overwrite with custom docker paths
resp,err := BuildRunContainer(2,"true")
resp,err := BuildRunContainer(2,"true","")
if err != nil {
t.Error(err)
@@ -15,3 +15,12 @@ func TestDocker(t *testing.T) {
fmt.Print(resp.VNCPort)
}
func TestViewAllContainers(t *testing.T) {
_,err := ViewAllContainers()
if err != nil {
t.Error(err)
}
}

View File

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

View File

@@ -104,7 +104,7 @@ func Server() error{
// Creates container and returns back result to
// access container
resp, err := docker.BuildRunContainer(PortsInt,GPU)
resp, err := docker.BuildRunContainer(PortsInt,GPU,"")
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
@@ -122,6 +122,15 @@ func Server() error{
c.String(http.StatusOK, "success")
})
//Show images avaliable
r.GET("/ShowImages", func(c *gin.Context) {
resp, err := docker.ViewAllContainers()
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))
}
c.JSON(http.StatusOK, resp)
})
// Future feature
/*r.GET("/create_vm/:virtualization", func(c *gin.Context) {
virtualization := c.Param("virtualization")