Forráskód Böngészése

Implement docker resize with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
David Calavera 9 éve
szülő
commit
d1057e4c46
4 módosított fájl, 57 hozzáadás és 10 törlés
  1. 2 0
      api/client/client.go
  2. 28 0
      api/client/lib/resize.go
  3. 11 9
      api/client/utils.go
  4. 16 1
      api/types/client.go

+ 2 - 0
api/client/client.go

@@ -23,6 +23,7 @@ type apiClient interface {
 	ContainerExecAttach(execID string, config runconfig.ExecConfig) (types.HijackedResponse, error)
 	ContainerExecCreate(config runconfig.ExecConfig) (types.ContainerExecCreateResponse, error)
 	ContainerExecInspect(execID string) (types.ContainerExecInspect, error)
+	ContainerExecResize(options types.ResizeOptions) error
 	ContainerExecStart(execID string, config types.ExecStartCheck) error
 	ContainerExport(containerID string) (io.ReadCloser, error)
 	ContainerInspect(containerID string) (types.ContainerJSON, error)
@@ -33,6 +34,7 @@ type apiClient interface {
 	ContainerPause(containerID string) error
 	ContainerRemove(options types.ContainerRemoveOptions) error
 	ContainerRename(containerID, newContainerName string) error
+	ContainerResize(options types.ResizeOptions) error
 	ContainerRestart(containerID string, timeout int) error
 	ContainerStatPath(containerID, path string) (types.ContainerPathStat, error)
 	ContainerStats(containerID string, stream bool) (io.ReadCloser, error)

+ 28 - 0
api/client/lib/resize.go

@@ -0,0 +1,28 @@
+package lib
+
+import (
+	"net/url"
+	"strconv"
+
+	"github.com/docker/docker/api/types"
+)
+
+// ContainerResize changes the size of the tty for a container.
+func (cli *Client) ContainerResize(options types.ResizeOptions) error {
+	return cli.resize("/containers/"+options.ID, options.Height, options.Width)
+}
+
+// ContainerExecResize changes the size of the tty for an exec process running inside a container.
+func (cli *Client) ContainerExecResize(options types.ResizeOptions) error {
+	return cli.resize("/exec/"+options.ID, options.Height, options.Width)
+}
+
+func (cli *Client) resize(basePath string, height, width int) error {
+	query := url.Values{}
+	query.Set("h", strconv.Itoa(height))
+	query.Set("w", strconv.Itoa(width))
+
+	resp, err := cli.post(basePath+"/resize", query, nil, nil)
+	ensureReaderClosed(resp)
+	return err
+}

+ 11 - 9
api/client/utils.go

@@ -9,17 +9,16 @@ import (
 	"io"
 	"io/ioutil"
 	"net/http"
-	"net/url"
 	"os"
 	gosignal "os/signal"
 	"runtime"
-	"strconv"
 	"strings"
 	"time"
 
 	"github.com/Sirupsen/logrus"
 	"github.com/docker/docker/api"
 	"github.com/docker/docker/api/client/lib"
+	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/cliconfig"
 	"github.com/docker/docker/dockerversion"
 	"github.com/docker/docker/pkg/jsonmessage"
@@ -259,18 +258,21 @@ func (cli *DockerCli) resizeTty(id string, isExec bool) {
 	if height == 0 && width == 0 {
 		return
 	}
-	v := url.Values{}
-	v.Set("h", strconv.Itoa(height))
-	v.Set("w", strconv.Itoa(width))
 
-	path := ""
+	options := types.ResizeOptions{
+		ID:     id,
+		Height: height,
+		Width:  width,
+	}
+
+	var err error
 	if !isExec {
-		path = "/containers/" + id + "/resize?"
+		err = cli.client.ContainerExecResize(options)
 	} else {
-		path = "/exec/" + id + "/resize?"
+		err = cli.client.ContainerResize(options)
 	}
 
-	if _, _, err := readBody(cli.call("POST", path+v.Encode(), nil, nil)); err != nil {
+	if err != nil {
 		logrus.Debugf("Error resize: %s", err)
 	}
 }

+ 16 - 1
api/types/client.go

@@ -197,7 +197,13 @@ type ImageRemoveOptions struct {
 	PruneChildren bool
 }
 
-// ImageTagOptions hold parameters to tag an image
+// ImageSearchOptions holds parameters to search images with.
+type ImageSearchOptions struct {
+	Term         string
+	RegistryAuth string
+}
+
+// ImageTagOptions holds parameters to tag an image
 type ImageTagOptions struct {
 	ImageID        string
 	RepositoryName string
@@ -205,6 +211,15 @@ type ImageTagOptions struct {
 	Force          bool
 }
 
+// ResizeOptions holds parameters to resize a tty.
+// It can be used to resize container ttys and
+// exec process ttys too.
+type ResizeOptions struct {
+	ID     string
+	Height int
+	Width  int
+}
+
 // VersionResponse holds version information for the client and the server
 type VersionResponse struct {
 	Client *Version