2014-03-28 23:21:55 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-12-12 04:11:42 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2014-03-28 23:21:55 +00:00
|
|
|
"fmt"
|
2015-12-14 01:25:28 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-03-28 23:21:55 +00:00
|
|
|
"os"
|
|
|
|
gosignal "os/signal"
|
2015-12-14 01:25:28 +00:00
|
|
|
"path/filepath"
|
2015-03-26 22:52:16 +00:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2014-03-28 23:21:55 +00:00
|
|
|
|
2016-03-16 19:19:22 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-11-13 18:40:22 +00:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
"github.com/docker/docker/registry"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/client"
|
|
|
|
"github.com/docker/engine-api/types"
|
|
|
|
registrytypes "github.com/docker/engine-api/types/registry"
|
2014-03-28 23:21:55 +00:00
|
|
|
)
|
|
|
|
|
2016-02-03 18:30:17 +00:00
|
|
|
func (cli *DockerCli) electAuthServer() string {
|
|
|
|
// The daemon `/info` endpoint informs us of the default registry being
|
|
|
|
// used. This is essential in cross-platforms environment, where for
|
|
|
|
// example a Linux client might be interacting with a Windows daemon, hence
|
|
|
|
// the default registry URL might be Windows specific.
|
|
|
|
serverAddress := registry.IndexServer
|
2016-03-16 19:19:22 +00:00
|
|
|
if info, err := cli.client.Info(context.Background()); err != nil {
|
2016-02-03 18:30:17 +00:00
|
|
|
fmt.Fprintf(cli.out, "Warning: failed to get default registry endpoint from daemon (%v). Using system default: %s\n", err, serverAddress)
|
|
|
|
} else {
|
|
|
|
serverAddress = info.IndexServerAddress
|
|
|
|
}
|
|
|
|
return serverAddress
|
|
|
|
}
|
|
|
|
|
2015-12-12 04:11:42 +00:00
|
|
|
// encodeAuthToBase64 serializes the auth configuration as JSON base64 payload
|
2015-12-12 02:14:52 +00:00
|
|
|
func encodeAuthToBase64(authConfig types.AuthConfig) (string, error) {
|
2015-12-12 04:11:42 +00:00
|
|
|
buf, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return base64.URLEncoding.EncodeToString(buf), nil
|
|
|
|
}
|
|
|
|
|
2016-04-13 08:33:46 +00:00
|
|
|
func (cli *DockerCli) registryAuthenticationPrivilegedFunc(index *registrytypes.IndexInfo, cmdName string) types.RequestPrivilegeFunc {
|
2015-12-06 20:17:34 +00:00
|
|
|
return func() (string, error) {
|
|
|
|
fmt.Fprintf(cli.out, "\nPlease login prior to %s:\n", cmdName)
|
2016-01-27 23:26:48 +00:00
|
|
|
indexServer := registry.GetAuthConfigKey(index)
|
2016-03-01 01:51:36 +00:00
|
|
|
authConfig, err := cli.configureAuth("", "", indexServer, false)
|
2016-01-27 23:26:48 +00:00
|
|
|
if err != nil {
|
2015-12-06 20:17:34 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2016-01-27 23:26:48 +00:00
|
|
|
return encodeAuthToBase64(authConfig)
|
2015-12-06 20:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 06:43:43 +00:00
|
|
|
func (cli *DockerCli) resizeTty(id string, isExec bool) {
|
2014-03-28 23:21:55 +00:00
|
|
|
height, width := cli.getTtySize()
|
2016-01-22 11:35:55 +00:00
|
|
|
cli.resizeTtyTo(id, height, width, isExec)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) resizeTtyTo(id string, height, width int, isExec bool) {
|
2014-03-28 23:21:55 +00:00
|
|
|
if height == 0 && width == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-09-16 06:43:43 +00:00
|
|
|
|
2015-12-06 23:41:57 +00:00
|
|
|
options := types.ResizeOptions{
|
|
|
|
Height: height,
|
|
|
|
Width: width,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2015-12-10 03:55:01 +00:00
|
|
|
if isExec {
|
2016-04-13 08:33:46 +00:00
|
|
|
err = cli.client.ContainerExecResize(context.Background(), id, options)
|
2014-09-16 06:43:43 +00:00
|
|
|
} else {
|
2016-04-13 08:33:46 +00:00
|
|
|
err = cli.client.ContainerResize(context.Background(), id, options)
|
2014-09-16 06:43:43 +00:00
|
|
|
}
|
|
|
|
|
2015-12-06 23:41:57 +00:00
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error resize: %s", err)
|
2014-03-28 23:21:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getExitCode perform an inspect on the container. It returns
|
|
|
|
// the running state and the exit code.
|
2015-03-26 02:31:29 +00:00
|
|
|
func getExitCode(cli *DockerCli, containerID string) (bool, int, error) {
|
2016-03-16 19:19:22 +00:00
|
|
|
c, err := cli.client.ContainerInspect(context.Background(), containerID)
|
2014-03-28 23:21:55 +00:00
|
|
|
if err != nil {
|
|
|
|
// If we can't connect, then the daemon probably died.
|
2016-01-05 00:05:26 +00:00
|
|
|
if err != client.ErrConnectionFailed {
|
2014-03-28 23:21:55 +00:00
|
|
|
return false, -1, err
|
|
|
|
}
|
|
|
|
return false, -1, nil
|
|
|
|
}
|
2014-06-04 22:11:11 +00:00
|
|
|
|
2015-04-13 14:17:14 +00:00
|
|
|
return c.State.Running, c.State.ExitCode, nil
|
2014-03-28 23:21:55 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:50:09 +00:00
|
|
|
// getExecExitCode perform an inspect on the exec command. It returns
|
|
|
|
// the running state and the exit code.
|
2015-03-26 02:31:29 +00:00
|
|
|
func getExecExitCode(cli *DockerCli, execID string) (bool, int, error) {
|
2016-03-16 19:19:22 +00:00
|
|
|
resp, err := cli.client.ContainerExecInspect(context.Background(), execID)
|
2014-11-17 23:50:09 +00:00
|
|
|
if err != nil {
|
|
|
|
// If we can't connect, then the daemon probably died.
|
2016-01-05 00:05:26 +00:00
|
|
|
if err != client.ErrConnectionFailed {
|
2014-11-17 23:50:09 +00:00
|
|
|
return false, -1, err
|
|
|
|
}
|
|
|
|
return false, -1, nil
|
|
|
|
}
|
|
|
|
|
2015-12-06 05:33:38 +00:00
|
|
|
return resp.Running, resp.ExitCode, nil
|
2014-11-17 23:50:09 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 06:43:43 +00:00
|
|
|
func (cli *DockerCli) monitorTtySize(id string, isExec bool) error {
|
|
|
|
cli.resizeTty(id, isExec)
|
2014-03-28 23:21:55 +00:00
|
|
|
|
2015-03-26 22:52:16 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
go func() {
|
2015-04-06 21:31:42 +00:00
|
|
|
prevH, prevW := cli.getTtySize()
|
2015-03-26 22:52:16 +00:00
|
|
|
for {
|
|
|
|
time.Sleep(time.Millisecond * 250)
|
2015-04-06 21:31:42 +00:00
|
|
|
h, w := cli.getTtySize()
|
2015-03-26 22:52:16 +00:00
|
|
|
|
|
|
|
if prevW != w || prevH != h {
|
|
|
|
cli.resizeTty(id, isExec)
|
|
|
|
}
|
|
|
|
prevH = h
|
2015-04-06 21:31:42 +00:00
|
|
|
prevW = w
|
2015-03-26 22:52:16 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
gosignal.Notify(sigchan, signal.SIGWINCH)
|
|
|
|
go func() {
|
2015-04-01 22:39:37 +00:00
|
|
|
for range sigchan {
|
2015-03-26 22:52:16 +00:00
|
|
|
cli.resizeTty(id, isExec)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-03-28 23:21:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *DockerCli) getTtySize() (int, int) {
|
2014-09-10 13:35:48 +00:00
|
|
|
if !cli.isTerminalOut {
|
2014-03-28 23:21:55 +00:00
|
|
|
return 0, 0
|
|
|
|
}
|
2014-09-10 13:35:48 +00:00
|
|
|
ws, err := term.GetWinsize(cli.outFd)
|
2014-03-28 23:21:55 +00:00
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Error getting size: %s", err)
|
2014-03-28 23:21:55 +00:00
|
|
|
if ws == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return int(ws.Height), int(ws.Width)
|
|
|
|
}
|
2015-12-14 01:25:28 +00:00
|
|
|
|
|
|
|
func copyToFile(outfile string, r io.Reader) error {
|
|
|
|
tmpFile, err := ioutil.TempFile(filepath.Dir(outfile), ".docker_temp_")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpPath := tmpFile.Name()
|
|
|
|
|
|
|
|
_, err = io.Copy(tmpFile, r)
|
|
|
|
tmpFile.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(tmpPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.Rename(tmpPath, outfile); err != nil {
|
|
|
|
os.Remove(tmpPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-03 18:55:33 +00:00
|
|
|
|
|
|
|
// resolveAuthConfig is like registry.ResolveAuthConfig, but if using the
|
|
|
|
// default index, it uses the default index name for the daemon's platform,
|
|
|
|
// not the client's platform.
|
2016-02-08 00:55:17 +00:00
|
|
|
func (cli *DockerCli) resolveAuthConfig(index *registrytypes.IndexInfo) types.AuthConfig {
|
2016-02-03 18:55:33 +00:00
|
|
|
configKey := index.Name
|
|
|
|
if index.Official {
|
|
|
|
configKey = cli.electAuthServer()
|
|
|
|
}
|
|
|
|
|
2016-02-08 00:55:17 +00:00
|
|
|
a, _ := getCredentials(cli.configFile, configKey)
|
|
|
|
return a
|
2016-02-03 18:55:33 +00:00
|
|
|
}
|
2016-03-01 17:04:35 +00:00
|
|
|
|
|
|
|
func (cli *DockerCli) retrieveAuthConfigs() map[string]types.AuthConfig {
|
|
|
|
acs, _ := getAllCredentials(cli.configFile)
|
|
|
|
return acs
|
|
|
|
}
|