2016-12-09 04:17:53 -05:00
|
|
|
package daemon
|
2016-03-04 03:29:24 -05:00
|
|
|
|
|
|
|
import (
|
2016-06-13 22:54:20 -04:00
|
|
|
"bytes"
|
2016-12-09 04:17:53 -05:00
|
|
|
"crypto/tls"
|
2016-03-04 03:29:24 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-12-09 04:17:53 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
2016-03-04 03:29:24 -05:00
|
|
|
"net/http"
|
2016-12-09 04:17:53 -05:00
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2016-03-04 03:29:24 -05:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2016-03-04 03:29:24 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2016-12-09 04:17:53 -05:00
|
|
|
"github.com/docker/docker/pkg/integration"
|
2016-03-04 03:29:24 -05:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2016-12-09 04:17:53 -05:00
|
|
|
icmd "github.com/docker/docker/pkg/integration/cmd"
|
2016-03-04 03:29:24 -05:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2016-09-06 18:08:14 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-03-04 03:29:24 -05:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2016-09-02 21:27:20 -04:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2016-03-04 03:29:24 -05:00
|
|
|
"github.com/go-check/check"
|
2016-12-09 04:18:02 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-03-04 03:29:24 -05:00
|
|
|
)
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// SockRoot holds the path of the default docker integration daemon socket
|
|
|
|
var SockRoot = filepath.Join(os.TempDir(), "docker-integration")
|
2016-07-28 10:19:09 -04:00
|
|
|
|
2016-03-04 03:29:24 -05:00
|
|
|
// Daemon represents a Docker daemon for the testing framework.
|
|
|
|
type Daemon struct {
|
2016-12-09 04:17:53 -05:00
|
|
|
GlobalFlags []string
|
|
|
|
Root string
|
|
|
|
Folder string
|
|
|
|
Wait chan error
|
|
|
|
UseDefaultHost bool
|
|
|
|
UseDefaultTLSHost bool
|
|
|
|
|
|
|
|
id string
|
|
|
|
logFile *os.File
|
|
|
|
stdin io.WriteCloser
|
|
|
|
stdout, stderr io.ReadCloser
|
|
|
|
cmd *exec.Cmd
|
|
|
|
storageDriver string
|
|
|
|
userlandProxy bool
|
|
|
|
execRoot string
|
|
|
|
experimental bool
|
|
|
|
dockerBinary string
|
|
|
|
dockerdBinary string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds docker daemon integration configuration
|
|
|
|
type Config struct {
|
|
|
|
Experimental bool
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientConfig struct {
|
|
|
|
transport *http.Transport
|
|
|
|
scheme string
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// New returns a Daemon instance to be used for testing.
|
2016-03-04 03:29:24 -05:00
|
|
|
// This will create a directory such as d123456789 in the folder specified by $DEST.
|
|
|
|
// The daemon will not automatically start.
|
2016-12-09 04:17:53 -05:00
|
|
|
func New(c *check.C, dockerBinary string, dockerdBinary string, config Config) *Daemon {
|
2016-03-04 03:29:24 -05:00
|
|
|
dest := os.Getenv("DEST")
|
|
|
|
c.Assert(dest, check.Not(check.Equals), "", check.Commentf("Please set the DEST environment variable"))
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
err := os.MkdirAll(SockRoot, 0700)
|
2016-07-28 10:19:09 -04:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("could not create daemon socket root"))
|
|
|
|
|
2016-09-06 18:08:14 -04:00
|
|
|
id := fmt.Sprintf("d%s", stringid.TruncateID(stringid.GenerateRandomID()))
|
2016-03-04 03:29:24 -05:00
|
|
|
dir := filepath.Join(dest, id)
|
|
|
|
daemonFolder, err := filepath.Abs(dir)
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf("Could not make %q an absolute path", dir))
|
|
|
|
daemonRoot := filepath.Join(daemonFolder, "root")
|
|
|
|
|
|
|
|
c.Assert(os.MkdirAll(daemonRoot, 0755), check.IsNil, check.Commentf("Could not create daemon root %q", dir))
|
|
|
|
|
|
|
|
userlandProxy := true
|
|
|
|
if env := os.Getenv("DOCKER_USERLANDPROXY"); env != "" {
|
|
|
|
if val, err := strconv.ParseBool(env); err != nil {
|
|
|
|
userlandProxy = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Daemon{
|
|
|
|
id: id,
|
2016-12-09 04:17:53 -05:00
|
|
|
Folder: daemonFolder,
|
|
|
|
Root: daemonRoot,
|
2016-03-04 03:29:24 -05:00
|
|
|
storageDriver: os.Getenv("DOCKER_GRAPHDRIVER"),
|
|
|
|
userlandProxy: userlandProxy,
|
2016-08-19 16:06:28 -04:00
|
|
|
execRoot: filepath.Join(os.TempDir(), "docker-execroot", id),
|
2016-12-09 04:17:53 -05:00
|
|
|
dockerBinary: dockerBinary,
|
|
|
|
dockerdBinary: dockerdBinary,
|
|
|
|
experimental: config.Experimental,
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-22 14:21:34 -05:00
|
|
|
// RootDir returns the root directory of the daemon.
|
|
|
|
func (d *Daemon) RootDir() string {
|
2016-12-09 04:17:53 -05:00
|
|
|
return d.Root
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the generated id of the daemon
|
|
|
|
func (d *Daemon) ID() string {
|
|
|
|
return d.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// StorageDriver returns the configured storage driver of the daemon
|
|
|
|
func (d *Daemon) StorageDriver() string {
|
|
|
|
return d.storageDriver
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanupExecRoot cleans the daemon exec root (network namespaces, ...)
|
|
|
|
func (d *Daemon) CleanupExecRoot(c *check.C) {
|
|
|
|
cleanupExecRoot(c, d.execRoot)
|
2016-11-22 14:21:34 -05:00
|
|
|
}
|
|
|
|
|
2016-03-04 03:29:24 -05:00
|
|
|
func (d *Daemon) getClientConfig() (*clientConfig, error) {
|
|
|
|
var (
|
|
|
|
transport *http.Transport
|
|
|
|
scheme string
|
|
|
|
addr string
|
|
|
|
proto string
|
|
|
|
)
|
2016-12-09 04:17:53 -05:00
|
|
|
if d.UseDefaultTLSHost {
|
2016-03-04 03:29:24 -05:00
|
|
|
option := &tlsconfig.Options{
|
|
|
|
CAFile: "fixtures/https/ca.pem",
|
|
|
|
CertFile: "fixtures/https/client-cert.pem",
|
|
|
|
KeyFile: "fixtures/https/client-key.pem",
|
|
|
|
}
|
|
|
|
tlsConfig, err := tlsconfig.Client(*option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
transport = &http.Transport{
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
addr = fmt.Sprintf("%s:%d", opts.DefaultHTTPHost, opts.DefaultTLSHTTPPort)
|
|
|
|
scheme = "https"
|
|
|
|
proto = "tcp"
|
2016-12-09 04:17:53 -05:00
|
|
|
} else if d.UseDefaultHost {
|
2016-03-04 03:29:24 -05:00
|
|
|
addr = opts.DefaultUnixSocket
|
|
|
|
proto = "unix"
|
|
|
|
scheme = "http"
|
|
|
|
transport = &http.Transport{}
|
|
|
|
} else {
|
2016-07-28 10:19:09 -04:00
|
|
|
addr = d.sockPath()
|
2016-03-04 03:29:24 -05:00
|
|
|
proto = "unix"
|
|
|
|
scheme = "http"
|
|
|
|
transport = &http.Transport{}
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:18:02 -05:00
|
|
|
if err := sockets.ConfigureTransport(transport, proto, addr); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-04 03:29:24 -05:00
|
|
|
|
|
|
|
return &clientConfig{
|
|
|
|
transport: transport,
|
|
|
|
scheme: scheme,
|
|
|
|
addr: addr,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start will start the daemon and return once it is ready to receive requests.
|
|
|
|
// You can specify additional daemon flags.
|
|
|
|
func (d *Daemon) Start(args ...string) error {
|
2016-12-09 04:17:53 -05:00
|
|
|
logFile, err := os.OpenFile(filepath.Join(d.Folder, "docker.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
|
2016-12-09 04:18:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "[%s] Could not create %s/docker.log", d.id, d.Folder)
|
|
|
|
}
|
2016-03-04 03:29:24 -05:00
|
|
|
|
|
|
|
return d.StartWithLogFile(logFile, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartWithLogFile will start the daemon and attach its streams to a given file.
|
|
|
|
func (d *Daemon) StartWithLogFile(out *os.File, providedArgs ...string) error {
|
2016-12-09 04:17:53 -05:00
|
|
|
dockerdBinary, err := exec.LookPath(d.dockerdBinary)
|
2016-12-09 04:18:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "[%s] could not find docker binary in $PATH", d.id)
|
|
|
|
}
|
2016-03-04 03:29:24 -05:00
|
|
|
|
|
|
|
args := append(d.GlobalFlags,
|
2016-03-22 20:55:47 -04:00
|
|
|
"--containerd", "/var/run/docker/libcontainerd/docker-containerd.sock",
|
2016-12-09 04:17:53 -05:00
|
|
|
"--graph", d.Root,
|
2016-08-19 16:06:28 -04:00
|
|
|
"--exec-root", d.execRoot,
|
2016-12-09 04:17:53 -05:00
|
|
|
"--pidfile", fmt.Sprintf("%s/docker.pid", d.Folder),
|
2016-03-04 03:29:24 -05:00
|
|
|
fmt.Sprintf("--userland-proxy=%t", d.userlandProxy),
|
|
|
|
)
|
2016-12-09 04:17:53 -05:00
|
|
|
if d.experimental {
|
2016-11-03 12:47:50 -04:00
|
|
|
args = append(args, "--experimental", "--init")
|
2016-10-06 10:09:54 -04:00
|
|
|
}
|
2016-12-09 04:17:53 -05:00
|
|
|
if !(d.UseDefaultHost || d.UseDefaultTLSHost) {
|
|
|
|
args = append(args, []string{"--host", d.Sock()}...)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
|
|
|
|
args = append(args, []string{"--userns-remap", root}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't explicitly set the log-level or debug flag(-D) then
|
|
|
|
// turn on debug mode
|
|
|
|
foundLog := false
|
|
|
|
foundSd := false
|
|
|
|
for _, a := range providedArgs {
|
|
|
|
if strings.Contains(a, "--log-level") || strings.Contains(a, "-D") || strings.Contains(a, "--debug") {
|
|
|
|
foundLog = true
|
|
|
|
}
|
|
|
|
if strings.Contains(a, "--storage-driver") {
|
|
|
|
foundSd = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundLog {
|
|
|
|
args = append(args, "--debug")
|
|
|
|
}
|
|
|
|
if d.storageDriver != "" && !foundSd {
|
|
|
|
args = append(args, "--storage-driver", d.storageDriver)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, providedArgs...)
|
2016-05-26 07:14:35 -04:00
|
|
|
d.cmd = exec.Command(dockerdBinary, args...)
|
2016-06-20 20:44:45 -04:00
|
|
|
d.cmd.Env = append(os.Environ(), "DOCKER_SERVICE_PREFER_OFFLINE_IMAGE=1")
|
2016-03-04 03:29:24 -05:00
|
|
|
d.cmd.Stdout = out
|
|
|
|
d.cmd.Stderr = out
|
|
|
|
d.logFile = out
|
|
|
|
|
|
|
|
if err := d.cmd.Start(); err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("[%s] could not start daemon container: %v", d.id, err)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
wait := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wait <- d.cmd.Wait()
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("[%s] exiting daemon\n", d.id)
|
2016-03-04 03:29:24 -05:00
|
|
|
close(wait)
|
|
|
|
}()
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
d.Wait = wait
|
2016-03-04 03:29:24 -05:00
|
|
|
|
|
|
|
tick := time.Tick(500 * time.Millisecond)
|
|
|
|
// make sure daemon is ready to receive requests
|
|
|
|
startTime := time.Now().Unix()
|
|
|
|
for {
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("[%s] waiting for daemon to start\n", d.id)
|
2016-03-04 03:29:24 -05:00
|
|
|
if time.Now().Unix()-startTime > 5 {
|
|
|
|
// After 5 seconds, give up
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("[%s] Daemon exited and never started", d.id)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-time.After(2 * time.Second):
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("[%s] timeout: daemon does not respond", d.id)
|
2016-03-04 03:29:24 -05:00
|
|
|
case <-tick:
|
|
|
|
clientConfig, err := d.getClientConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: clientConfig.transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/_ping", nil)
|
2016-12-09 04:18:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "[%s] could not create new request", d.id)
|
|
|
|
}
|
2016-03-04 03:29:24 -05:00
|
|
|
req.URL.Host = clientConfig.addr
|
|
|
|
req.URL.Scheme = clientConfig.scheme
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("[%s] received status != 200 OK: %s\n", d.id, resp.Status)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("[%s] daemon started\n", d.id)
|
2016-12-09 04:17:53 -05:00
|
|
|
d.Root, err = d.queryRootDir()
|
2016-03-04 03:29:24 -05:00
|
|
|
if err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("[%s] error querying daemon for root directory: %v", d.id, err)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
return nil
|
2016-12-09 04:17:53 -05:00
|
|
|
case <-d.Wait:
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("[%s] Daemon exited during startup", d.id)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartWithBusybox will first start the daemon with Daemon.Start()
|
|
|
|
// then save the busybox image from the main daemon and load it into this Daemon instance.
|
|
|
|
func (d *Daemon) StartWithBusybox(arg ...string) error {
|
|
|
|
if err := d.Start(arg...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return d.LoadBusybox()
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:50:19 -04:00
|
|
|
// Kill will send a SIGKILL to the daemon
|
|
|
|
func (d *Daemon) Kill() error {
|
2016-12-09 04:17:53 -05:00
|
|
|
if d.cmd == nil || d.Wait == nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
return errors.New("daemon not started")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
d.logFile.Close()
|
|
|
|
d.cmd = nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := d.cmd.Process.Kill(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
if err := os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder)); err != nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// Pid returns the pid of the daemon
|
|
|
|
func (d *Daemon) Pid() int {
|
|
|
|
return d.cmd.Process.Pid
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interrupt stops the daemon by sending it an Interrupt signal
|
|
|
|
func (d *Daemon) Interrupt() error {
|
|
|
|
return d.Signal(os.Interrupt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signal sends the specified signal to the daemon if running
|
|
|
|
func (d *Daemon) Signal(signal os.Signal) error {
|
|
|
|
if d.cmd == nil || d.Wait == nil {
|
|
|
|
return errors.New("daemon not started")
|
|
|
|
}
|
|
|
|
return d.cmd.Process.Signal(signal)
|
|
|
|
}
|
|
|
|
|
2016-07-27 14:17:44 -04:00
|
|
|
// DumpStackAndQuit sends SIGQUIT to the daemon, which triggers it to dump its
|
|
|
|
// stack to its log file and exit
|
|
|
|
// This is used primarily for gathering debug information on test timeout
|
|
|
|
func (d *Daemon) DumpStackAndQuit() {
|
|
|
|
if d.cmd == nil || d.cmd.Process == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-12-09 04:17:53 -05:00
|
|
|
SignalDaemonDump(d.cmd.Process.Pid)
|
2016-07-27 14:17:44 -04:00
|
|
|
}
|
|
|
|
|
2016-03-04 03:29:24 -05:00
|
|
|
// Stop will send a SIGINT every second and wait for the daemon to stop.
|
|
|
|
// If it timeouts, a SIGKILL is sent.
|
|
|
|
// Stop will not delete the daemon directory. If a purged daemon is needed,
|
|
|
|
// instantiate a new one with NewDaemon.
|
|
|
|
func (d *Daemon) Stop() error {
|
2016-12-09 04:17:53 -05:00
|
|
|
if d.cmd == nil || d.Wait == nil {
|
2016-03-04 03:29:24 -05:00
|
|
|
return errors.New("daemon not started")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
d.logFile.Close()
|
|
|
|
d.cmd = nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
i := 1
|
|
|
|
tick := time.Tick(time.Second)
|
|
|
|
|
|
|
|
if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("could not send signal: %v", err)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
out1:
|
|
|
|
for {
|
|
|
|
select {
|
2016-12-09 04:17:53 -05:00
|
|
|
case err := <-d.Wait:
|
2016-03-04 03:29:24 -05:00
|
|
|
return err
|
2016-06-13 22:54:20 -04:00
|
|
|
case <-time.After(20 * time.Second):
|
2016-03-04 03:29:24 -05:00
|
|
|
// time for stopping jobs and run onShutdown hooks
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("timeout: %v\n", d.id)
|
2016-03-04 03:29:24 -05:00
|
|
|
break out1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out2:
|
|
|
|
for {
|
|
|
|
select {
|
2016-12-09 04:17:53 -05:00
|
|
|
case err := <-d.Wait:
|
2016-03-04 03:29:24 -05:00
|
|
|
return err
|
|
|
|
case <-tick:
|
|
|
|
i++
|
2016-06-13 22:54:20 -04:00
|
|
|
if i > 5 {
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("tried to interrupt daemon for %d times, now try to kill it\n", i)
|
2016-03-04 03:29:24 -05:00
|
|
|
break out2
|
|
|
|
}
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("Attempt #%d: daemon is still running with pid %d\n", i, d.cmd.Process.Pid)
|
2016-03-04 03:29:24 -05:00
|
|
|
if err := d.cmd.Process.Signal(os.Interrupt); err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("could not send signal: %v", err)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.cmd.Process.Kill(); err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
fmt.Printf("Could not kill daemon: %v\n", err)
|
2016-03-04 03:29:24 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
if err := os.Remove(fmt.Sprintf("%s/docker.pid", d.Folder)); err != nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-04 03:29:24 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart will restart the daemon by first stopping it and then starting it.
|
|
|
|
func (d *Daemon) Restart(arg ...string) error {
|
|
|
|
d.Stop()
|
|
|
|
// in the case of tests running a user namespace-enabled daemon, we have resolved
|
2016-12-09 04:17:53 -05:00
|
|
|
// d.Root to be the actual final path of the graph dir after the "uid.gid" of
|
2016-03-04 03:29:24 -05:00
|
|
|
// remapped root is added--we need to subtract it from the path before calling
|
|
|
|
// start or else we will continue making subdirectories rather than truly restarting
|
|
|
|
// with the same location/root:
|
|
|
|
if root := os.Getenv("DOCKER_REMAP_ROOT"); root != "" {
|
2016-12-09 04:17:53 -05:00
|
|
|
d.Root = filepath.Dir(d.Root)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
return d.Start(arg...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadBusybox will load the stored busybox into a newly started daemon
|
|
|
|
func (d *Daemon) LoadBusybox() error {
|
2016-12-09 04:17:53 -05:00
|
|
|
bb := filepath.Join(d.Folder, "busybox.tar")
|
2016-03-04 03:29:24 -05:00
|
|
|
if _, err := os.Stat(bb); err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("unexpected error on busybox.tar stat: %v", err)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
// saving busybox image from main daemon
|
2016-12-09 04:17:53 -05:00
|
|
|
if out, err := exec.Command(d.dockerBinary, "save", "--output", bb, "busybox:latest").CombinedOutput(); err != nil {
|
|
|
|
imagesOut, _ := exec.Command(d.dockerBinary, "images", "--format", "{{ .Repository }}:{{ .Tag }}").CombinedOutput()
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("could not save busybox image: %s\n%s", string(out), strings.TrimSpace(string(imagesOut)))
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// loading busybox image to this daemon
|
|
|
|
if out, err := d.Cmd("load", "--input", bb); err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("could not load busybox image: %s", out)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
if err := os.Remove(bb); err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return err
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) queryRootDir() (string, error) {
|
|
|
|
// update daemon root by asking /info endpoint (to support user
|
|
|
|
// namespaced daemon with root remapped uid.gid directory)
|
|
|
|
clientConfig, err := d.getClientConfig()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: clientConfig.transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/info", nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.URL.Host = clientConfig.addr
|
|
|
|
req.URL.Scheme = clientConfig.scheme
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
|
|
|
return resp.Body.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
type Info struct {
|
|
|
|
DockerRootDir string
|
|
|
|
}
|
|
|
|
var b []byte
|
|
|
|
var i Info
|
2016-12-09 04:17:53 -05:00
|
|
|
b, err = integration.ReadBody(body)
|
2016-07-19 03:40:20 -04:00
|
|
|
if err == nil && resp.StatusCode == http.StatusOK {
|
2016-03-04 03:29:24 -05:00
|
|
|
// read the docker root dir
|
|
|
|
if err = json.Unmarshal(b, &i); err == nil {
|
|
|
|
return i.DockerRootDir, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// Sock returns the socket path of the daemon
|
|
|
|
func (d *Daemon) Sock() string {
|
2016-07-28 10:19:09 -04:00
|
|
|
return fmt.Sprintf("unix://" + d.sockPath())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) sockPath() string {
|
2016-12-09 04:17:53 -05:00
|
|
|
return filepath.Join(SockRoot, d.id+".sock")
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// WaitRun waits for a container to be running for 10s
|
|
|
|
func (d *Daemon) WaitRun(contID string) error {
|
|
|
|
args := []string{"--host", d.Sock()}
|
|
|
|
return WaitInspectWithArgs(d.dockerBinary, contID, "{{.State.Running}}", "true", 10*time.Second, args...)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// GetBaseDeviceSize returns the base device size of the daemon
|
|
|
|
func (d *Daemon) GetBaseDeviceSize(c *check.C) int64 {
|
|
|
|
infoCmdOutput, _, err := integration.RunCommandPipelineWithOutput(
|
|
|
|
exec.Command(d.dockerBinary, "-H", d.Sock(), "info"),
|
2016-03-04 03:29:24 -05:00
|
|
|
exec.Command("grep", "Base Device Size"),
|
|
|
|
)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
basesizeSlice := strings.Split(infoCmdOutput, ":")
|
|
|
|
basesize := strings.Trim(basesizeSlice[1], " ")
|
|
|
|
basesize = strings.Trim(basesize, "\n")[:len(basesize)-3]
|
|
|
|
basesizeFloat, err := strconv.ParseFloat(strings.Trim(basesize, " "), 64)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
basesizeBytes := int64(basesizeFloat) * (1024 * 1024 * 1024)
|
|
|
|
return basesizeBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cmd will execute a docker CLI command against this Daemon.
|
|
|
|
// Example: d.Cmd("version") will run docker -H unix://path/to/unix.sock version
|
2016-07-22 12:14:05 -04:00
|
|
|
func (d *Daemon) Cmd(args ...string) (string, error) {
|
2016-12-09 04:17:53 -05:00
|
|
|
b, err := d.Command(args...).CombinedOutput()
|
2016-03-04 03:29:24 -05:00
|
|
|
return string(b), err
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// Command will create a docker CLI command against this Daeomn.
|
|
|
|
func (d *Daemon) Command(args ...string) *exec.Cmd {
|
|
|
|
return exec.Command(d.dockerBinary, d.PrependHostArg(args)...)
|
2016-10-24 17:44:51 -04:00
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// PrependHostArg prepend the specified arguments by the daemon host flags
|
|
|
|
func (d *Daemon) PrependHostArg(args []string) []string {
|
2016-07-22 12:14:05 -04:00
|
|
|
for _, arg := range args {
|
|
|
|
if arg == "--host" || arg == "-H" {
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
}
|
2016-12-09 04:17:53 -05:00
|
|
|
return append([]string{"--host", d.Sock()}, args...)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:54:20 -04:00
|
|
|
// SockRequest executes a socket request on a daemon and returns statuscode and output.
|
|
|
|
func (d *Daemon) SockRequest(method, endpoint string, data interface{}) (int, []byte, error) {
|
|
|
|
jsonData := bytes.NewBuffer(nil)
|
|
|
|
if err := json.NewEncoder(jsonData).Encode(data); err != nil {
|
|
|
|
return -1, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, body, err := d.SockRequestRaw(method, endpoint, jsonData, "application/json")
|
|
|
|
if err != nil {
|
|
|
|
return -1, nil, err
|
|
|
|
}
|
2016-12-09 04:17:53 -05:00
|
|
|
b, err := integration.ReadBody(body)
|
2016-06-13 22:54:20 -04:00
|
|
|
return res.StatusCode, b, err
|
|
|
|
}
|
|
|
|
|
2016-11-18 02:51:36 -05:00
|
|
|
// SockRequestRaw executes a socket request on a daemon and returns an http
|
2016-06-13 22:54:20 -04:00
|
|
|
// response and a reader for the output data.
|
|
|
|
func (d *Daemon) SockRequestRaw(method, endpoint string, data io.Reader, ct string) (*http.Response, io.ReadCloser, error) {
|
2016-12-09 04:17:53 -05:00
|
|
|
return SockRequestRawToDaemon(method, endpoint, data, ct, d.Sock())
|
2016-06-13 22:54:20 -04:00
|
|
|
}
|
|
|
|
|
2016-03-04 03:29:24 -05:00
|
|
|
// LogFileName returns the path the the daemon's log file
|
|
|
|
func (d *Daemon) LogFileName() string {
|
|
|
|
return d.logFile.Name()
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// GetIDByName returns the ID of an object (container, volume, …) given its name
|
|
|
|
func (d *Daemon) GetIDByName(name string) (string, error) {
|
2016-03-04 03:29:24 -05:00
|
|
|
return d.inspectFieldWithError(name, "Id")
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// ActiveContainers returns the list of ids of the currently running containers
|
|
|
|
func (d *Daemon) ActiveContainers() (ids []string) {
|
|
|
|
// FIXME(vdemeester) shouldn't ignore the error
|
2016-06-13 22:54:20 -04:00
|
|
|
out, _ := d.Cmd("ps", "-q")
|
|
|
|
for _, id := range strings.Split(out, "\n") {
|
|
|
|
if id = strings.TrimSpace(id); id != "" {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// ReadLogFile returns the content of the daemon log file
|
|
|
|
func (d *Daemon) ReadLogFile() ([]byte, error) {
|
|
|
|
return ioutil.ReadFile(d.logFile.Name())
|
|
|
|
}
|
|
|
|
|
2016-03-04 03:29:24 -05:00
|
|
|
func (d *Daemon) inspectFilter(name, filter string) (string, error) {
|
|
|
|
format := fmt.Sprintf("{{%s}}", filter)
|
|
|
|
out, err := d.Cmd("inspect", "-f", format, name)
|
|
|
|
if err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return "", errors.Errorf("failed to inspect %s: %s", name, out)
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
|
|
|
return strings.TrimSpace(out), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) inspectFieldWithError(name, field string) (string, error) {
|
|
|
|
return d.inspectFilter(name, fmt.Sprintf(".%s", field))
|
|
|
|
}
|
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// FindContainerIP returns the ip of the specified container
|
2016-12-09 04:18:02 -05:00
|
|
|
func (d *Daemon) FindContainerIP(id string) (string, error) {
|
|
|
|
out, err := d.Cmd("inspect", "--format='{{ .NetworkSettings.Networks.bridge.IPAddress }}'", id)
|
2016-03-04 03:29:24 -05:00
|
|
|
if err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return "", err
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
2016-12-09 04:18:02 -05:00
|
|
|
return strings.Trim(out, " \r\n'"), nil
|
2016-03-04 03:29:24 -05:00
|
|
|
}
|
2016-05-24 04:13:54 -04:00
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// BuildImageWithOut builds an image with the specified dockerfile and options and returns the output
|
|
|
|
func (d *Daemon) BuildImageWithOut(name, dockerfile string, useCache bool, buildFlags ...string) (string, int, error) {
|
|
|
|
buildCmd := BuildImageCmdWithHost(d.dockerBinary, name, dockerfile, d.Sock(), useCache, buildFlags...)
|
|
|
|
result := icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: buildCmd.Args,
|
|
|
|
Env: buildCmd.Env,
|
|
|
|
Dir: buildCmd.Dir,
|
|
|
|
Stdin: buildCmd.Stdin,
|
|
|
|
Stdout: buildCmd.Stdout,
|
|
|
|
})
|
|
|
|
return result.Combined(), result.ExitCode, result.Error
|
2016-05-24 04:13:54 -04:00
|
|
|
}
|
2016-06-13 22:54:20 -04:00
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// CheckActiveContainerCount returns the number of active containers
|
|
|
|
// FIXME(vdemeester) should re-use ActivateContainers in some way
|
|
|
|
func (d *Daemon) CheckActiveContainerCount(c *check.C) (interface{}, check.CommentInterface) {
|
2016-06-13 22:54:20 -04:00
|
|
|
out, err := d.Cmd("ps", "-q")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
if len(strings.TrimSpace(out)) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return len(strings.Split(strings.TrimSpace(out), "\n")), check.Commentf("output: %q", string(out))
|
|
|
|
}
|
2016-07-28 11:58:06 -04:00
|
|
|
|
2016-12-09 04:17:53 -05:00
|
|
|
// ReloadConfig asks the daemon to reload its configuration
|
|
|
|
func (d *Daemon) ReloadConfig() error {
|
2016-07-28 11:58:06 -04:00
|
|
|
if d.cmd == nil || d.cmd.Process == nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.New("daemon is not running")
|
2016-07-28 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
started := make(chan struct{})
|
|
|
|
go func() {
|
2016-12-09 04:17:53 -05:00
|
|
|
_, body, err := SockRequestRawToDaemon("GET", "/events", nil, "", d.Sock())
|
2016-07-28 11:58:06 -04:00
|
|
|
close(started)
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
}
|
|
|
|
defer body.Close()
|
|
|
|
dec := json.NewDecoder(body)
|
|
|
|
for {
|
|
|
|
var e events.Message
|
|
|
|
if err := dec.Decode(&e); err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if e.Type != events.DaemonEventType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e.Action != "reload" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
close(errCh) // notify that we are done
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-started
|
|
|
|
if err := signalDaemonReload(d.cmd.Process.Pid); err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("error signaling daemon reload: %v", err)
|
2016-07-28 11:58:06 -04:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("error waiting for daemon reload event: %v", err)
|
2016-07-28 11:58:06 -04:00
|
|
|
}
|
|
|
|
case <-time.After(30 * time.Second):
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.New("timeout waiting for daemon reload event")
|
2016-07-28 11:58:06 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-09 04:17:53 -05:00
|
|
|
|
|
|
|
// WaitInspectWithArgs waits for the specified expression to be equals to the specified expected string in the given time.
|
|
|
|
// FIXME(vdemeester) Attach this to the Daemon struct
|
|
|
|
func WaitInspectWithArgs(dockerBinary, name, expr, expected string, timeout time.Duration, arg ...string) error {
|
|
|
|
after := time.After(timeout)
|
|
|
|
|
|
|
|
args := append(arg, "inspect", "-f", expr, name)
|
|
|
|
for {
|
|
|
|
result := icmd.RunCommand(dockerBinary, args...)
|
|
|
|
if result.Error != nil {
|
|
|
|
if !strings.Contains(result.Stderr(), "No such") {
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("error executing docker inspect: %v\n%s",
|
2016-12-09 04:17:53 -05:00
|
|
|
result.Stderr(), result.Stdout())
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
return result.Error
|
|
|
|
default:
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out := strings.TrimSpace(result.Stdout())
|
|
|
|
if out == expected {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-after:
|
2016-12-09 04:18:02 -05:00
|
|
|
return errors.Errorf("condition \"%q == %q\" not true in time (%v)", out, expected, timeout)
|
2016-12-09 04:17:53 -05:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SockRequestRawToDaemon creates an http request against the specified daemon socket
|
|
|
|
// FIXME(vdemeester) attach this to daemon ?
|
|
|
|
func SockRequestRawToDaemon(method, endpoint string, data io.Reader, ct, daemon string) (*http.Response, io.ReadCloser, error) {
|
|
|
|
req, client, err := newRequestClient(method, endpoint, data, ct, daemon)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
client.Close()
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
body := ioutils.NewReadCloserWrapper(resp.Body, func() error {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return client.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
return resp, body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTLSConfig() (*tls.Config, error) {
|
|
|
|
dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
|
|
|
|
|
|
|
|
if dockerCertPath == "" {
|
2016-12-09 04:18:02 -05:00
|
|
|
return nil, errors.New("DOCKER_TLS_VERIFY specified, but no DOCKER_CERT_PATH environment variable")
|
2016-12-09 04:17:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
option := &tlsconfig.Options{
|
|
|
|
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
|
|
|
|
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
|
|
|
|
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
|
|
|
|
}
|
|
|
|
tlsConfig, err := tlsconfig.Client(*option)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SockConn opens a connection on the specified socket
|
|
|
|
func SockConn(timeout time.Duration, daemon string) (net.Conn, error) {
|
|
|
|
daemonURL, err := url.Parse(daemon)
|
|
|
|
if err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return nil, errors.Wrapf(err, "could not parse url %q", daemon)
|
2016-12-09 04:17:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var c net.Conn
|
|
|
|
switch daemonURL.Scheme {
|
|
|
|
case "npipe":
|
|
|
|
return npipeDial(daemonURL.Path, timeout)
|
|
|
|
case "unix":
|
|
|
|
return net.DialTimeout(daemonURL.Scheme, daemonURL.Path, timeout)
|
|
|
|
case "tcp":
|
|
|
|
if os.Getenv("DOCKER_TLS_VERIFY") != "" {
|
|
|
|
// Setup the socket TLS configuration.
|
|
|
|
tlsConfig, err := getTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dialer := &net.Dialer{Timeout: timeout}
|
|
|
|
return tls.DialWithDialer(dialer, daemonURL.Scheme, daemonURL.Host, tlsConfig)
|
|
|
|
}
|
|
|
|
return net.DialTimeout(daemonURL.Scheme, daemonURL.Host, timeout)
|
|
|
|
default:
|
2016-12-09 04:18:02 -05:00
|
|
|
return c, errors.Errorf("unknown scheme %v (%s)", daemonURL.Scheme, daemon)
|
2016-12-09 04:17:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequestClient(method, endpoint string, data io.Reader, ct, daemon string) (*http.Request, *httputil.ClientConn, error) {
|
|
|
|
c, err := SockConn(time.Duration(10*time.Second), daemon)
|
|
|
|
if err != nil {
|
2016-12-09 04:18:02 -05:00
|
|
|
return nil, nil, errors.Errorf("could not dial docker daemon: %v", err)
|
2016-12-09 04:17:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
client := httputil.NewClientConn(c, nil)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, endpoint, data)
|
|
|
|
if err != nil {
|
|
|
|
client.Close()
|
2016-12-09 04:18:02 -05:00
|
|
|
return nil, nil, errors.Errorf("could not create new request: %v", err)
|
2016-12-09 04:17:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ct != "" {
|
|
|
|
req.Header.Set("Content-Type", ct)
|
|
|
|
}
|
|
|
|
return req, client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildImageCmdWithHost create a build command with the specified arguments.
|
|
|
|
// FIXME(vdemeester) move this away
|
|
|
|
func BuildImageCmdWithHost(dockerBinary, name, dockerfile, host string, useCache bool, buildFlags ...string) *exec.Cmd {
|
|
|
|
args := []string{}
|
|
|
|
if host != "" {
|
|
|
|
args = append(args, "--host", host)
|
|
|
|
}
|
|
|
|
args = append(args, "build", "-t", name)
|
|
|
|
if !useCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
|
|
|
args = append(args, buildFlags...)
|
|
|
|
args = append(args, "-")
|
|
|
|
buildCmd := exec.Command(dockerBinary, args...)
|
|
|
|
buildCmd.Stdin = strings.NewReader(dockerfile)
|
|
|
|
return buildCmd
|
|
|
|
}
|