added code to detect docker containers
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Simple Ubuntu 20.04 image
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user