From d1057e4c4672b584590295f3d9b96a90183bbfcd Mon Sep 17 00:00:00 2001 From: David Calavera Date: Sun, 6 Dec 2015 18:41:57 -0500 Subject: [PATCH] Implement docker resize with standalone client lib. Signed-off-by: David Calavera --- api/client/client.go | 2 ++ api/client/lib/resize.go | 28 ++++++++++++++++++++++++++++ api/client/utils.go | 24 +++++++++++++----------- api/types/client.go | 17 ++++++++++++++++- 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 api/client/lib/resize.go diff --git a/api/client/client.go b/api/client/client.go index 7d7a69ffd2..e3be35beb6 100644 --- a/api/client/client.go +++ b/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) diff --git a/api/client/lib/resize.go b/api/client/lib/resize.go new file mode 100644 index 0000000000..dffd81d544 --- /dev/null +++ b/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 +} diff --git a/api/client/utils.go b/api/client/utils.go index 095ae8ee63..40b14efac1 100644 --- a/api/client/utils.go +++ b/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 := "" - if !isExec { - path = "/containers/" + id + "/resize?" - } else { - path = "/exec/" + id + "/resize?" + options := types.ResizeOptions{ + ID: id, + Height: height, + Width: width, } - if _, _, err := readBody(cli.call("POST", path+v.Encode(), nil, nil)); err != nil { + var err error + if !isExec { + err = cli.client.ContainerExecResize(options) + } else { + err = cli.client.ContainerResize(options) + } + + if err != nil { logrus.Debugf("Error resize: %s", err) } } diff --git a/api/types/client.go b/api/types/client.go index 698dc48543..a89b952319 100644 --- a/api/types/client.go +++ b/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