diff --git a/config/config.go b/config/config.go index b30a11d..c51d04a 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/server/docker/containers/docker-ubuntu-sshd/description.txt b/server/docker/containers/docker-ubuntu-sshd/description.txt new file mode 100644 index 0000000..dc5b3fa --- /dev/null +++ b/server/docker/containers/docker-ubuntu-sshd/description.txt @@ -0,0 +1 @@ +Simple Ubuntu 20.04 image \ No newline at end of file diff --git a/server/docker/docker.go b/server/docker/docker.go index 10dd606..20ffbda 100644 --- a/server/docker/docker.go +++ b/server/docker/docker.go @@ -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 diff --git a/server/docker/docker_test.go b/server/docker/docker_test.go index 50f39b7..53921e2 100644 --- a/server/docker/docker_test.go +++ b/server/docker/docker_test.go @@ -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) + } + +} diff --git a/server/rpc.go b/server/rpc.go index eb8efad..cdd21f4 100644 --- a/server/rpc.go +++ b/server/rpc.go @@ -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 } diff --git a/server/server.go b/server/server.go index b0b11d5..354c392 100644 --- a/server/server.go +++ b/server/server.go @@ -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")