diff --git a/client/container.go b/client/container.go index 9e57def..b4ca8f2 100644 --- a/client/container.go +++ b/client/container.go @@ -19,9 +19,9 @@ var client = http.Client{} // StartContainer Start container using REST api Implementation // From the selected server IP address // TODO: Test cases for this function -func StartContainer(IP string, NumPorts int, GPU bool) (*docker.DockerVM ,error) { +func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string) (*docker.DockerVM ,error) { // Passes URL with number of TCP ports to allocated and to give GPU access to the docker container - URL := "http://" + IP + ":" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + URL := "http://" + IP + ":" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName resp, err := http.Get(URL) if err != nil { return nil,err diff --git a/cmd/action.go b/cmd/action.go index 62022fd..9785fe4 100644 --- a/cmd/action.go +++ b/cmd/action.go @@ -81,7 +81,8 @@ var CliAction = func(ctx *cli.Context) error { fmt.Sscanf(Ports, "%d", &PortsInt) } - imageRes, err := client.StartContainer(CreateVM,PortsInt,GPU) + // Calls function to do Api call to start the container on the server side + imageRes, err := client.StartContainer(CreateVM,PortsInt,GPU,ContainerName) if err != nil { fmt.Print(err) diff --git a/cmd/flags.go b/cmd/flags.go index 2b5a97f..fc6c4fe 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -9,6 +9,7 @@ var ( AddServer string ViewImages string CreateVM string + ContainerName string Ports string Mode string RemoveVM string @@ -59,6 +60,12 @@ var AppConfigFlags = []cli.Flag{ EnvVars: []string{"CREATE_VM"}, Destination: &CreateVM, }, + &cli.StringFlag{ + Name: "ContainerName", + Usage: "Specifying the container run on the server side", + EnvVars: []string{"CONTAINER_NAME"}, + Destination: &ContainerName, + }, &cli.StringFlag{ Name: "RemoveVM", Usage: "Stop and Remove Docker container", diff --git a/server/docker/docker.go b/server/docker/docker.go index 20ffbda..7b53652 100644 --- a/server/docker/docker.go +++ b/server/docker/docker.go @@ -93,6 +93,22 @@ func BuildRunContainer(NumPorts int, GPU string, ContainerName string) (*DockerV } RespDocker.ImagePath = config.DefaultDockerFile + if ContainerName != "" { + Containers, err := ViewAllContainers() + if err != nil { + return nil,err + } + for _, dockerContainer := range Containers.DockerContainer { + if dockerContainer.ContainerName == ContainerName { + RespDocker.ImagePath = config.DockerContainers + ContainerName + "/" + break + } + } + if RespDocker.ImagePath == config.DefaultDockerFile { + return nil, errors.New("Container " + ContainerName + " does not exist in the server") + } + } + // Gets docker information from env variables cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { diff --git a/server/server.go b/server/server.go index e166c1c..7f847ff 100644 --- a/server/server.go +++ b/server/server.go @@ -97,6 +97,7 @@ func Server() error{ // Get Number of ports to open and whether to use GPU or not Ports := c.DefaultQuery("ports","0") GPU := c.DefaultQuery("GPU","false") + ContainerName := c.DefaultQuery("ContainerName","") var PortsInt int // Convert Get Request value to int @@ -104,7 +105,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,ContainerName) if err != nil { c.String(http.StatusInternalServerError, fmt.Sprintf("error: %s", err))