Implement docker resize with standalone client lib.

Signed-off-by: David Calavera <david.calavera@gmail.com>
This commit is contained in:
David Calavera 2015-12-06 18:41:57 -05:00
parent cf3efd05d4
commit d1057e4c46
4 changed files with 59 additions and 12 deletions

View File

@ -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
api/client/lib/resize.go Normal file
View File

@ -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
}

View File

@ -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)
}
}

View File

@ -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