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) ContainerExecAttach(execID string, config runconfig.ExecConfig) (types.HijackedResponse, error)
ContainerExecCreate(config runconfig.ExecConfig) (types.ContainerExecCreateResponse, error) ContainerExecCreate(config runconfig.ExecConfig) (types.ContainerExecCreateResponse, error)
ContainerExecInspect(execID string) (types.ContainerExecInspect, error) ContainerExecInspect(execID string) (types.ContainerExecInspect, error)
ContainerExecResize(options types.ResizeOptions) error
ContainerExecStart(execID string, config types.ExecStartCheck) error ContainerExecStart(execID string, config types.ExecStartCheck) error
ContainerExport(containerID string) (io.ReadCloser, error) ContainerExport(containerID string) (io.ReadCloser, error)
ContainerInspect(containerID string) (types.ContainerJSON, error) ContainerInspect(containerID string) (types.ContainerJSON, error)
@ -33,6 +34,7 @@ type apiClient interface {
ContainerPause(containerID string) error ContainerPause(containerID string) error
ContainerRemove(options types.ContainerRemoveOptions) error ContainerRemove(options types.ContainerRemoveOptions) error
ContainerRename(containerID, newContainerName string) error ContainerRename(containerID, newContainerName string) error
ContainerResize(options types.ResizeOptions) error
ContainerRestart(containerID string, timeout int) error ContainerRestart(containerID string, timeout int) error
ContainerStatPath(containerID, path string) (types.ContainerPathStat, error) ContainerStatPath(containerID, path string) (types.ContainerPathStat, error)
ContainerStats(containerID string, stream bool) (io.ReadCloser, 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"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"os" "os"
gosignal "os/signal" gosignal "os/signal"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/docker/api" "github.com/docker/docker/api"
"github.com/docker/docker/api/client/lib" "github.com/docker/docker/api/client/lib"
"github.com/docker/docker/api/types"
"github.com/docker/docker/cliconfig" "github.com/docker/docker/cliconfig"
"github.com/docker/docker/dockerversion" "github.com/docker/docker/dockerversion"
"github.com/docker/docker/pkg/jsonmessage" "github.com/docker/docker/pkg/jsonmessage"
@ -259,18 +258,21 @@ func (cli *DockerCli) resizeTty(id string, isExec bool) {
if height == 0 && width == 0 { if height == 0 && width == 0 {
return return
} }
v := url.Values{}
v.Set("h", strconv.Itoa(height))
v.Set("w", strconv.Itoa(width))
path := "" options := types.ResizeOptions{
if !isExec { ID: id,
path = "/containers/" + id + "/resize?" Height: height,
} else { Width: width,
path = "/exec/" + id + "/resize?"
} }
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) logrus.Debugf("Error resize: %s", err)
} }
} }

View File

@ -197,7 +197,13 @@ type ImageRemoveOptions struct {
PruneChildren bool 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 { type ImageTagOptions struct {
ImageID string ImageID string
RepositoryName string RepositoryName string
@ -205,6 +211,15 @@ type ImageTagOptions struct {
Force bool 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 // VersionResponse holds version information for the client and the server
type VersionResponse struct { type VersionResponse struct {
Client *Version Client *Version