2014-04-17 17:43:01 -04:00
|
|
|
package daemon
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2013-08-15 14:38:17 -04:00
|
|
|
"errors"
|
2013-03-21 03:25:00 -04:00
|
|
|
"fmt"
|
2014-04-28 17:36:04 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-05-11 16:49:46 -04:00
|
|
|
"path/filepath"
|
2014-04-28 17:36:04 -04:00
|
|
|
"strings"
|
2015-05-05 16:25:05 -04:00
|
|
|
"sync"
|
2014-04-28 17:36:04 -04:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2014-07-24 16:37:44 -04:00
|
|
|
"github.com/docker/libcontainer/label"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-02-04 14:04:58 -05:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
"github.com/docker/docker/daemon/logger/jsonfilelog"
|
2015-04-04 00:06:48 -04:00
|
|
|
"github.com/docker/docker/daemon/network"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/nat"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-07-30 11:16:10 -04:00
|
|
|
"github.com/docker/docker/pkg/broadcastwriter"
|
2014-08-12 12:10:43 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-05-05 16:25:05 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2015-05-19 16:05:25 -04:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2014-09-30 02:16:27 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
|
|
|
"github.com/docker/docker/runconfig"
|
2015-05-19 16:05:25 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2013-11-29 10:40:44 -05:00
|
|
|
var (
|
2014-02-14 20:15:40 -05:00
|
|
|
ErrNotATTY = errors.New("The PTY is not a file")
|
|
|
|
ErrNoTTY = errors.New("No PTY found")
|
|
|
|
ErrContainerStart = errors.New("The container failed to start. Unknown error")
|
|
|
|
ErrContainerStartTimeout = errors.New("The container failed to start due to timed out.")
|
2013-11-29 10:40:44 -05:00
|
|
|
)
|
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
type StreamConfig struct {
|
2014-08-26 18:05:37 -04:00
|
|
|
stdout *broadcastwriter.BroadcastWriter
|
|
|
|
stderr *broadcastwriter.BroadcastWriter
|
|
|
|
stdin io.ReadCloser
|
|
|
|
stdinPipe io.WriteCloser
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:53:35 -04:00
|
|
|
// CommonContainer holds the settings for a container which are applicable
|
|
|
|
// across all platforms supported by the daemon.
|
|
|
|
type CommonContainer struct {
|
2015-05-19 16:05:25 -04:00
|
|
|
StreamConfig
|
|
|
|
|
2014-09-05 17:35:55 -04:00
|
|
|
*State `json:"State"` // Needed for remote api version <= 1.11
|
|
|
|
root string // Path to the "home" of the container, including metadata.
|
|
|
|
basefs string // Path to the graphdriver mountpoint
|
2013-03-21 03:25:00 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
ID string
|
|
|
|
Created time.Time
|
|
|
|
Path string
|
|
|
|
Args []string
|
|
|
|
Config *runconfig.Config
|
|
|
|
ImageID string `json:"Image"`
|
|
|
|
NetworkSettings *network.Settings
|
|
|
|
ResolvConfPath string
|
|
|
|
HostnamePath string
|
|
|
|
HostsPath string
|
|
|
|
LogPath string
|
|
|
|
Name string
|
|
|
|
Driver string
|
|
|
|
ExecDriver string
|
2014-04-21 17:09:26 -04:00
|
|
|
MountLabel, ProcessLabel string
|
2014-08-04 18:53:10 -04:00
|
|
|
RestartCount int
|
2014-12-10 00:55:09 -05:00
|
|
|
UpdateDns bool
|
2015-06-03 12:26:41 -04:00
|
|
|
|
|
|
|
MountPoints map[string]*mountPoint
|
|
|
|
Volumes map[string]string // Deprecated since 1.7, kept for backwards compatibility
|
|
|
|
VolumesRW map[string]bool // Deprecated since 1.7, kept for backwards compatibility
|
2013-04-09 10:57:59 -04:00
|
|
|
|
2014-02-11 23:04:39 -05:00
|
|
|
hostConfig *runconfig.HostConfig
|
2015-05-19 16:05:25 -04:00
|
|
|
command *execdriver.Command
|
2013-10-04 22:25:15 -04:00
|
|
|
|
2015-02-04 14:04:58 -05:00
|
|
|
monitor *containerMonitor
|
|
|
|
execCommands *execStore
|
2015-05-19 16:05:25 -04:00
|
|
|
daemon *Daemon
|
2015-02-04 14:04:58 -05:00
|
|
|
// logDriver for closing
|
2015-04-29 18:53:35 -04:00
|
|
|
logDriver logger.Logger
|
|
|
|
logCopier *logger.Copier
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2013-03-21 03:25:00 -04:00
|
|
|
func (container *Container) FromDisk() error {
|
2014-05-27 22:15:42 -04:00
|
|
|
pth, err := container.jsonPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-04 08:43:58 -05:00
|
|
|
jsonSource, err := os.Open(pth)
|
2013-01-25 17:39:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 08:43:58 -05:00
|
|
|
defer jsonSource.Close()
|
|
|
|
|
|
|
|
dec := json.NewDecoder(jsonSource)
|
|
|
|
|
2013-03-21 03:25:00 -04:00
|
|
|
// Load container settings
|
2013-07-29 12:40:35 -04:00
|
|
|
// udp broke compat of docker.PortMapping, but it's not used when loading a container, we can skip it
|
2014-11-04 08:43:58 -05:00
|
|
|
if err := dec.Decode(container); err != nil && !strings.Contains(err.Error(), "docker.PortMapping") {
|
2013-01-25 17:39:02 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-04-28 17:36:04 -04:00
|
|
|
|
|
|
|
if err := label.ReserveLabel(container.ProcessLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-31 17:58:43 -04:00
|
|
|
return container.readHostConfig()
|
2013-01-25 17:39:02 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 13:49:24 -04:00
|
|
|
func (container *Container) toDisk() error {
|
2013-01-22 14:13:22 -05:00
|
|
|
data, err := json.Marshal(container)
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
|
|
|
pth, err := container.jsonPath()
|
2013-10-31 17:58:43 -04:00
|
|
|
if err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:50:25 -04:00
|
|
|
if err := ioutil.WriteFile(pth, data, 0666); err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
2013-10-31 17:58:43 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2014-03-07 21:42:29 -05:00
|
|
|
return container.WriteHostConfig()
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 13:49:24 -04:00
|
|
|
func (container *Container) ToDisk() error {
|
|
|
|
container.Lock()
|
|
|
|
err := container.toDisk()
|
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
func (container *Container) readHostConfig() error {
|
2014-02-11 23:04:39 -05:00
|
|
|
container.hostConfig = &runconfig.HostConfig{}
|
2013-10-31 17:58:43 -04:00
|
|
|
// If the hostconfig file does not exist, do not read it.
|
|
|
|
// (We still have to initialize container.hostConfig,
|
|
|
|
// but that's OK, since we just did that above.)
|
2014-05-27 22:15:42 -04:00
|
|
|
pth, err := container.hostConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(pth)
|
2013-10-31 17:58:43 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-04-21 15:59:59 -04:00
|
|
|
f, err := os.Open(pth)
|
2013-07-02 08:19:25 -04:00
|
|
|
if err != nil {
|
2013-10-31 17:58:43 -04:00
|
|
|
return err
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
2015-04-21 15:59:59 -04:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return json.NewDecoder(f).Decode(&container.hostConfig)
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:15:42 -04:00
|
|
|
func (container *Container) WriteHostConfig() error {
|
2013-10-31 17:58:43 -04:00
|
|
|
data, err := json.Marshal(container.hostConfig)
|
2013-07-02 08:19:25 -04:00
|
|
|
if err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
|
|
|
pth, err := container.hostConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(pth, data, 0666)
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
|
|
|
|
2014-07-31 07:50:59 -04:00
|
|
|
func (container *Container) LogEvent(action string) {
|
|
|
|
d := container.daemon
|
2015-04-03 18:17:49 -04:00
|
|
|
d.EventsService.Log(
|
|
|
|
action,
|
|
|
|
container.ID,
|
2015-03-28 22:17:17 -04:00
|
|
|
container.Config.Image,
|
2015-04-03 18:17:49 -04:00
|
|
|
)
|
2014-07-31 07:50:59 -04:00
|
|
|
}
|
|
|
|
|
2015-03-05 20:53:06 -05:00
|
|
|
// Evaluates `path` in the scope of the container's basefs, with proper path
|
|
|
|
// sanitisation. Symlinks are all scoped to the basefs of the container, as
|
|
|
|
// though the container's basefs was `/`.
|
|
|
|
//
|
|
|
|
// The basefs of a container is the host-facing path which is bind-mounted as
|
|
|
|
// `/` inside the container. This method is essentially used to access a
|
|
|
|
// particular path inside the container as though you were a process in that
|
|
|
|
// container.
|
|
|
|
//
|
|
|
|
// NOTE: The returned path is *only* safely scoped inside the container's basefs
|
|
|
|
// if no component of the returned path changes (such as a component
|
|
|
|
// symlinking to a different path) between using this method and using the
|
|
|
|
// path. See symlink.FollowSymlinkInScope for more details.
|
|
|
|
func (container *Container) GetResourcePath(path string) (string, error) {
|
2015-06-01 19:42:27 -04:00
|
|
|
// IMPORTANT - These are paths on the OS where the daemon is running, hence
|
|
|
|
// any filepath operations must be done in an OS agnostic way.
|
|
|
|
cleanPath := filepath.Join(string(os.PathSeparator), path)
|
|
|
|
r, e := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, cleanPath), container.basefs)
|
|
|
|
return r, e
|
2014-05-11 16:49:46 -04:00
|
|
|
}
|
|
|
|
|
2015-03-05 20:53:06 -05:00
|
|
|
// Evaluates `path` in the scope of the container's root, with proper path
|
|
|
|
// sanitisation. Symlinks are all scoped to the root of the container, as
|
|
|
|
// though the container's root was `/`.
|
|
|
|
//
|
|
|
|
// The root of a container is the host-facing configuration metadata directory.
|
|
|
|
// Only use this method to safely access the container's `container.json` or
|
|
|
|
// other metadata files. If in doubt, use container.GetResourcePath.
|
|
|
|
//
|
|
|
|
// NOTE: The returned path is *only* safely scoped inside the container's root
|
|
|
|
// if no component of the returned path changes (such as a component
|
|
|
|
// symlinking to a different path) between using this method and using the
|
|
|
|
// path. See symlink.FollowSymlinkInScope for more details.
|
|
|
|
func (container *Container) GetRootResourcePath(path string) (string, error) {
|
2015-06-01 19:42:27 -04:00
|
|
|
// IMPORTANT - These are paths on the OS where the daemon is running, hence
|
|
|
|
// any filepath operations must be done in an OS agnostic way.
|
|
|
|
cleanPath := filepath.Join(string(os.PathSeparator), path)
|
2014-05-28 19:12:29 -04:00
|
|
|
return symlink.FollowSymlinkInScope(filepath.Join(container.root, cleanPath), container.root)
|
2014-05-11 16:49:46 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
func (container *Container) Start() (err error) {
|
2013-11-21 15:21:03 -05:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
if container.Running {
|
2014-03-30 01:12:34 -04:00
|
|
|
return nil
|
2013-11-11 21:15:38 -05:00
|
|
|
}
|
2014-08-04 18:53:10 -04:00
|
|
|
|
2015-03-12 15:26:17 -04:00
|
|
|
if container.removalInProgress || container.Dead {
|
|
|
|
return fmt.Errorf("Container is marked for removal and cannot be started.")
|
|
|
|
}
|
|
|
|
|
2015-01-27 20:23:57 -05:00
|
|
|
// if we encounter an error during start we need to ensure that any other
|
2014-04-14 02:40:49 -04:00
|
|
|
// setup has been cleaned up properly
|
2013-10-16 16:12:56 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2014-09-30 04:30:58 -04:00
|
|
|
container.setError(err)
|
2014-11-05 21:23:42 -05:00
|
|
|
// if no one else has set it, make sure we don't leave it at zero
|
|
|
|
if container.ExitCode == 0 {
|
|
|
|
container.ExitCode = 128
|
|
|
|
}
|
2014-09-30 04:30:58 -04:00
|
|
|
container.toDisk()
|
2013-10-16 16:12:56 -04:00
|
|
|
container.cleanup()
|
2015-05-24 14:19:39 -04:00
|
|
|
container.LogEvent("die")
|
2013-10-16 15:01:55 -04:00
|
|
|
}
|
2013-10-16 16:12:56 -04:00
|
|
|
}()
|
2014-01-29 21:34:43 -05:00
|
|
|
|
2013-12-06 06:15:14 -05:00
|
|
|
if err := container.Mount(); err != nil {
|
2013-10-16 16:12:56 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-04-14 02:40:49 -04:00
|
|
|
if err := container.initializeNetworking(); err != nil {
|
|
|
|
return err
|
2013-10-16 16:12:56 -04:00
|
|
|
}
|
2014-04-14 02:40:49 -04:00
|
|
|
linkedEnv, err := container.setupLinkedContainers()
|
2013-10-04 22:25:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-14 02:40:49 -04:00
|
|
|
if err := container.setupWorkingDirectory(); err != nil {
|
2014-01-30 10:43:53 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-05-05 17:45:14 -04:00
|
|
|
env := container.createDaemonEnvironment(linkedEnv)
|
2014-04-30 18:46:56 -04:00
|
|
|
if err := populateCommand(container, env); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
|
|
|
|
mounts, err := container.setupMounts()
|
|
|
|
if err != nil {
|
2013-10-16 16:12:56 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-01-13 18:37:17 -05:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
container.command.Mounts = mounts
|
2014-04-14 02:40:49 -04:00
|
|
|
return container.waitForStart()
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) Run() error {
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-01-18 19:13:39 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) Output() (output []byte, err error) {
|
2014-12-04 16:12:29 -05:00
|
|
|
pipe := container.StdoutPipe()
|
2013-01-18 19:13:39 -05:00
|
|
|
defer pipe.Close()
|
2013-10-31 17:58:43 -04:00
|
|
|
if err := container.Start(); err != nil {
|
2013-01-18 19:13:39 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
output, err = ioutil.ReadAll(pipe)
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-01-18 19:13:39 -05:00
|
|
|
return output, err
|
|
|
|
}
|
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
// StreamConfig.StdinPipe returns a WriteCloser which can be used to feed data
|
2013-10-16 13:41:36 -04:00
|
|
|
// to the standard input of the container's active process.
|
|
|
|
// Container.StdoutPipe and Container.StderrPipe each return a ReadCloser
|
|
|
|
// which can be used to retrieve the standard output (and error) generated
|
|
|
|
// by the container's active process. The output (and error) are actually
|
|
|
|
// copied and delivered to all StdoutPipe and StderrPipe consumers, using
|
|
|
|
// a kind of "broadcaster".
|
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
func (streamConfig *StreamConfig) StdinPipe() io.WriteCloser {
|
|
|
|
return streamConfig.stdinPipe
|
2013-01-28 20:50:12 -05:00
|
|
|
}
|
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
func (streamConfig *StreamConfig) StdoutPipe() io.ReadCloser {
|
2013-01-18 19:13:39 -05:00
|
|
|
reader, writer := io.Pipe()
|
2014-08-26 18:44:00 -04:00
|
|
|
streamConfig.stdout.AddWriter(writer, "")
|
2014-12-04 16:12:29 -05:00
|
|
|
return ioutils.NewBufReader(reader)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2014-12-04 16:12:29 -05:00
|
|
|
func (streamConfig *StreamConfig) StderrPipe() io.ReadCloser {
|
2013-01-18 19:13:39 -05:00
|
|
|
reader, writer := io.Pipe()
|
2014-08-26 18:44:00 -04:00
|
|
|
streamConfig.stderr.AddWriter(writer, "")
|
2014-12-04 16:12:29 -05:00
|
|
|
return ioutils.NewBufReader(reader)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
func (streamConfig *StreamConfig) StdoutLogPipe() io.ReadCloser {
|
2014-04-02 15:26:06 -04:00
|
|
|
reader, writer := io.Pipe()
|
2014-08-26 18:44:00 -04:00
|
|
|
streamConfig.stdout.AddWriter(writer, "stdout")
|
2014-08-12 12:10:43 -04:00
|
|
|
return ioutils.NewBufReader(reader)
|
2014-04-02 15:26:06 -04:00
|
|
|
}
|
|
|
|
|
2014-08-26 18:44:00 -04:00
|
|
|
func (streamConfig *StreamConfig) StderrLogPipe() io.ReadCloser {
|
2014-04-02 15:26:06 -04:00
|
|
|
reader, writer := io.Pipe()
|
2014-08-26 18:44:00 -04:00
|
|
|
streamConfig.stderr.AddWriter(writer, "stderr")
|
2014-08-12 12:10:43 -04:00
|
|
|
return ioutils.NewBufReader(reader)
|
2014-04-02 15:26:06 -04:00
|
|
|
}
|
|
|
|
|
2014-09-29 19:56:10 -04:00
|
|
|
func (container *Container) isNetworkAllocated() bool {
|
|
|
|
return container.NetworkSettings.IPAddress != ""
|
|
|
|
}
|
|
|
|
|
2014-08-04 18:53:10 -04:00
|
|
|
// cleanup releases any network resources allocated to the container along with any rules
|
|
|
|
// around how containers are linked together. It also unmounts the container's root filesystem.
|
2013-10-16 15:01:55 -04:00
|
|
|
func (container *Container) cleanup() {
|
2014-10-09 18:15:17 -04:00
|
|
|
container.ReleaseNetwork()
|
|
|
|
|
2015-04-29 18:53:35 -04:00
|
|
|
disableAllActiveLinks(container)
|
2013-03-30 12:05:53 -04:00
|
|
|
|
2013-03-21 03:25:00 -04:00
|
|
|
if err := container.Unmount(); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Errorf("%v: Failed to umount filesystem: %v", container.ID, err)
|
2013-01-21 21:03:23 -05:00
|
|
|
}
|
2014-11-17 18:50:09 -05:00
|
|
|
|
|
|
|
for _, eConfig := range container.execCommands.s {
|
|
|
|
container.daemon.unregisterExecCommand(eConfig)
|
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
2015-05-26 17:28:26 -04:00
|
|
|
container.UnmountVolumes(false)
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2014-03-07 21:42:29 -05:00
|
|
|
func (container *Container) KillSig(sig int) error {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Sending %d to %s", sig, container.ID)
|
2013-11-21 15:21:03 -05:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2013-10-08 15:15:29 -04:00
|
|
|
|
2014-06-03 11:44:21 -04:00
|
|
|
// We could unpause the container for them rather than returning this error
|
2014-08-31 11:20:35 -04:00
|
|
|
if container.Paused {
|
2014-06-03 11:44:21 -04:00
|
|
|
return fmt.Errorf("Container %s is paused. Unpause the container before stopping", container.ID)
|
|
|
|
}
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
if !container.Running {
|
2015-04-13 11:17:07 -04:00
|
|
|
return fmt.Errorf("Container %s is not running", container.ID)
|
2013-03-23 22:51:35 -04:00
|
|
|
}
|
2014-08-04 20:05:56 -04:00
|
|
|
|
|
|
|
// signal to the monitor that it should not restart the container
|
|
|
|
// after we send the kill signal
|
|
|
|
container.monitor.ExitOnNext()
|
2014-08-04 18:53:10 -04:00
|
|
|
|
2014-08-11 14:35:18 -04:00
|
|
|
// if the container is currently restarting we do not need to send the signal
|
|
|
|
// to the process. Telling the monitor that it should exit on it's next event
|
|
|
|
// loop is enough
|
2014-08-31 11:20:35 -04:00
|
|
|
if container.Restarting {
|
2014-08-11 14:35:18 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-24 14:19:39 -04:00
|
|
|
if err := container.daemon.Kill(container, sig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.LogEvent("kill")
|
|
|
|
return nil
|
2013-10-08 15:15:29 -04:00
|
|
|
}
|
|
|
|
|
2015-01-19 07:44:04 -05:00
|
|
|
// Wrapper aroung KillSig() suppressing "no such process" error.
|
|
|
|
func (container *Container) killPossiblyDeadProcess(sig int) error {
|
|
|
|
err := container.KillSig(sig)
|
|
|
|
if err == syscall.ESRCH {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Debugf("Cannot kill process (pid=%d) with signal %d: no such process.", container.GetPid(), sig)
|
2015-01-19 07:44:04 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-21 17:06:18 -04:00
|
|
|
func (container *Container) Pause() error {
|
2015-05-09 05:01:07 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-05-30 15:32:29 -04:00
|
|
|
// We cannot Pause the container which is not running
|
|
|
|
if !container.Running {
|
|
|
|
return fmt.Errorf("Container %s is not running, cannot pause a non-running container", container.ID)
|
|
|
|
}
|
|
|
|
|
2015-05-09 05:01:07 -04:00
|
|
|
// We cannot Pause the container which is already paused
|
|
|
|
if container.Paused {
|
2014-05-21 17:06:18 -04:00
|
|
|
return fmt.Errorf("Container %s is already paused", container.ID)
|
|
|
|
}
|
2015-05-09 05:01:07 -04:00
|
|
|
|
|
|
|
if err := container.daemon.execDriver.Pause(container.command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.Paused = true
|
2015-05-24 14:19:39 -04:00
|
|
|
container.LogEvent("pause")
|
2015-05-09 05:01:07 -04:00
|
|
|
return nil
|
2014-05-21 17:06:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) Unpause() error {
|
2015-05-09 05:01:07 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
// We cannot unpause the container which is not running
|
|
|
|
if !container.Running {
|
2015-05-30 15:32:29 -04:00
|
|
|
return fmt.Errorf("Container %s is not running, cannot unpause a non-running container", container.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot unpause the container which is not paused
|
|
|
|
if !container.Paused {
|
|
|
|
return fmt.Errorf("Container %s is not paused", container.ID)
|
2014-05-21 17:06:18 -04:00
|
|
|
}
|
2015-05-09 05:01:07 -04:00
|
|
|
|
|
|
|
if err := container.daemon.execDriver.Unpause(container.command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.Paused = false
|
2015-05-24 14:19:39 -04:00
|
|
|
container.LogEvent("unpause")
|
2015-05-09 05:01:07 -04:00
|
|
|
return nil
|
2014-05-21 17:06:18 -04:00
|
|
|
}
|
|
|
|
|
2013-10-08 15:15:29 -04:00
|
|
|
func (container *Container) Kill() error {
|
2014-08-31 11:20:35 -04:00
|
|
|
if !container.IsRunning() {
|
2015-04-29 15:05:20 -04:00
|
|
|
return fmt.Errorf("Container %s is not running", container.ID)
|
2013-10-08 15:15:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Send SIGKILL
|
2015-01-19 07:44:04 -05:00
|
|
|
if err := container.killPossiblyDeadProcess(9); err != nil {
|
2015-05-24 23:05:10 -04:00
|
|
|
// While normally we might "return err" here we're not going to
|
|
|
|
// because if we can't stop the container by this point then
|
|
|
|
// its probably because its already stopped. Meaning, between
|
|
|
|
// the time of the IsRunning() call above and now it stopped.
|
|
|
|
// Also, since the err return will be exec driver specific we can't
|
|
|
|
// look for any particular (common) error that would indicate
|
|
|
|
// that the process is already dead vs something else going wrong.
|
|
|
|
// So, instead we'll give it up to 2 more seconds to complete and if
|
|
|
|
// by that time the container is still running, then the error
|
|
|
|
// we got is probably valid and so we return it to the caller.
|
|
|
|
|
|
|
|
if container.IsRunning() {
|
|
|
|
container.WaitStop(2 * time.Second)
|
|
|
|
if container.IsRunning() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2013-04-11 12:02:34 -04:00
|
|
|
|
|
|
|
// 2. Wait for the process to die, in last resort, try to kill the process directly
|
2015-04-29 18:53:35 -04:00
|
|
|
if err := killProcessDirectly(container); err != nil {
|
|
|
|
return err
|
2013-04-11 12:02:34 -04:00
|
|
|
}
|
|
|
|
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-01-21 21:03:23 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:43:44 -04:00
|
|
|
func (container *Container) Stop(seconds int) error {
|
2014-08-31 11:20:35 -04:00
|
|
|
if !container.IsRunning() {
|
2013-01-18 19:13:39 -05:00
|
|
|
return nil
|
|
|
|
}
|
2013-01-21 21:03:23 -05:00
|
|
|
|
|
|
|
// 1. Send a SIGTERM
|
2015-01-19 07:44:04 -05:00
|
|
|
if err := container.killPossiblyDeadProcess(15); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Infof("Failed to send SIGTERM to the process, force killing")
|
2015-01-19 07:44:04 -05:00
|
|
|
if err := container.killPossiblyDeadProcess(9); err != nil {
|
2014-03-24 08:03:56 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-01-21 21:03:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Wait for the process to exit on its own
|
2014-08-31 11:20:35 -04:00
|
|
|
if _, err := container.WaitStop(time.Duration(seconds) * time.Second); err != nil {
|
2015-03-26 18:22:04 -04:00
|
|
|
logrus.Infof("Container %v failed to exit within %d seconds of SIGTERM - using the force", container.ID, seconds)
|
2014-03-24 08:03:56 -04:00
|
|
|
// 3. If it doesn't, then send SIGKILL
|
|
|
|
if err := container.Kill(); err != nil {
|
2014-08-31 11:20:35 -04:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2014-03-24 08:03:56 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-01-21 21:03:23 -05:00
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
2015-05-24 14:19:39 -04:00
|
|
|
container.LogEvent("stop")
|
2013-01-18 19:13:39 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:43:44 -04:00
|
|
|
func (container *Container) Restart(seconds int) error {
|
2014-02-17 10:10:54 -05:00
|
|
|
// Avoid unnecessarily unmounting and then directly mounting
|
|
|
|
// the container when the container stops and then starts
|
|
|
|
// again
|
|
|
|
if err := container.Mount(); err == nil {
|
|
|
|
defer container.Unmount()
|
|
|
|
}
|
|
|
|
|
2013-04-16 12:43:44 -04:00
|
|
|
if err := container.Stop(seconds); err != nil {
|
2013-01-22 18:03:40 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-05-24 14:19:39 -04:00
|
|
|
|
|
|
|
if err := container.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container.LogEvent("restart")
|
|
|
|
return nil
|
2013-01-22 18:03:40 -05:00
|
|
|
}
|
|
|
|
|
2013-05-23 22:33:28 -04:00
|
|
|
func (container *Container) Resize(h, w int) error {
|
2014-10-23 16:30:18 -04:00
|
|
|
if !container.IsRunning() {
|
|
|
|
return fmt.Errorf("Cannot resize container %s, container is not running", container.ID)
|
|
|
|
}
|
2015-05-24 14:19:39 -04:00
|
|
|
if err := container.command.ProcessConfig.Terminal.Resize(h, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.LogEvent("resize")
|
|
|
|
return nil
|
2013-05-23 22:33:28 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 19:57:45 -04:00
|
|
|
func (container *Container) Export() (archive.Archive, error) {
|
2013-12-06 06:15:14 -05:00
|
|
|
if err := container.Mount(); err != nil {
|
2013-03-21 03:25:00 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-05 16:18:02 -05:00
|
|
|
|
2014-01-30 10:43:53 -05:00
|
|
|
archive, err := archive.Tar(container.basefs, archive.Uncompressed)
|
2013-12-05 16:18:02 -05:00
|
|
|
if err != nil {
|
2014-02-03 18:56:58 -05:00
|
|
|
container.Unmount()
|
2013-12-05 16:18:02 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-24 14:19:39 -04:00
|
|
|
arch := ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
container.Unmount()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
container.LogEvent("export")
|
|
|
|
return arch, err
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) Mount() error {
|
2014-04-17 17:43:01 -04:00
|
|
|
return container.daemon.Mount(container)
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
2014-09-12 03:44:25 -04:00
|
|
|
func (container *Container) changes() ([]archive.Change, error) {
|
|
|
|
return container.daemon.Changes(container)
|
|
|
|
}
|
|
|
|
|
2013-11-07 19:35:26 -05:00
|
|
|
func (container *Container) Changes() ([]archive.Change, error) {
|
2014-05-27 15:04:20 -04:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2014-09-12 03:44:25 -04:00
|
|
|
return container.changes()
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
2014-03-07 20:36:47 -05:00
|
|
|
func (container *Container) GetImage() (*image.Image, error) {
|
2014-04-17 17:43:01 -04:00
|
|
|
if container.daemon == nil {
|
2013-03-21 03:25:00 -04:00
|
|
|
return nil, fmt.Errorf("Can't get image of unregistered container")
|
|
|
|
}
|
2014-10-28 17:06:23 -04:00
|
|
|
return container.daemon.graph.Get(container.ImageID)
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) Unmount() error {
|
2014-04-17 17:43:01 -04:00
|
|
|
return container.daemon.Unmount(container)
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:15:42 -04:00
|
|
|
func (container *Container) hostConfigPath() (string, error) {
|
2015-03-05 20:53:06 -05:00
|
|
|
return container.GetRootResourcePath("hostconfig.json")
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
|
|
|
|
2014-05-27 22:15:42 -04:00
|
|
|
func (container *Container) jsonPath() (string, error) {
|
2015-03-05 20:53:06 -05:00
|
|
|
return container.GetRootResourcePath("config.json")
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method must be exported to be used from the lxc template
|
2014-01-30 10:43:53 -05:00
|
|
|
// This directory is only usable when the container is running
|
2013-03-21 03:25:00 -04:00
|
|
|
func (container *Container) RootfsPath() string {
|
2014-01-30 10:43:53 -05:00
|
|
|
return container.basefs
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func validateID(id string) error {
|
2013-03-21 03:25:00 -04:00
|
|
|
if id == "" {
|
|
|
|
return fmt.Errorf("Invalid empty id")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-13 09:10:26 -04:00
|
|
|
|
2014-02-11 09:21:33 -05:00
|
|
|
func (container *Container) Copy(resource string) (io.ReadCloser, error) {
|
2015-02-24 17:17:13 -05:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
var err error
|
2013-12-06 06:15:14 -05:00
|
|
|
if err := container.Mount(); err != nil {
|
2013-07-17 00:07:41 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-24 17:17:13 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-05-19 16:05:25 -04:00
|
|
|
// unmount any volumes
|
2015-05-22 13:37:00 -04:00
|
|
|
container.UnmountVolumes(true)
|
2015-05-19 16:05:25 -04:00
|
|
|
// unmount the container's rootfs
|
2015-02-24 17:17:13 -05:00
|
|
|
container.Unmount()
|
|
|
|
}
|
|
|
|
}()
|
2015-05-19 16:05:25 -04:00
|
|
|
mounts, err := container.setupMounts()
|
|
|
|
if err != nil {
|
2014-05-23 03:48:01 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
for _, m := range mounts {
|
|
|
|
dest, err := container.GetResourcePath(m.Destination)
|
2015-02-24 17:17:13 -05:00
|
|
|
if err != nil {
|
2015-05-19 16:05:25 -04:00
|
|
|
return nil, err
|
2014-09-24 09:07:11 -04:00
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
if err := mount.Mount(m.Source, dest, "bind", "rbind,ro"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-03-05 20:53:06 -05:00
|
|
|
basePath, err := container.GetResourcePath(resource)
|
2015-02-24 17:17:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-29 06:58:57 -04:00
|
|
|
}
|
2013-07-17 10:25:49 -04:00
|
|
|
stat, err := os.Stat(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-24 09:07:11 -04:00
|
|
|
var filter []string
|
2013-07-17 10:25:49 -04:00
|
|
|
if !stat.IsDir() {
|
2015-04-29 18:53:35 -04:00
|
|
|
d, f := filepath.Split(basePath)
|
2013-07-17 10:25:49 -04:00
|
|
|
basePath = d
|
|
|
|
filter = []string{f}
|
|
|
|
} else {
|
2015-04-29 18:53:35 -04:00
|
|
|
filter = []string{filepath.Base(basePath)}
|
|
|
|
basePath = filepath.Dir(basePath)
|
2013-07-17 10:25:49 -04:00
|
|
|
}
|
2014-02-13 19:05:36 -05:00
|
|
|
archive, err := archive.TarWithOptions(basePath, &archive.TarOptions{
|
2014-10-23 17:30:11 -04:00
|
|
|
Compression: archive.Uncompressed,
|
|
|
|
IncludeFiles: filter,
|
2013-11-11 17:30:38 -05:00
|
|
|
})
|
2013-12-05 16:18:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-24 14:19:39 -04:00
|
|
|
reader := ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
container.UnmountVolumes(true)
|
|
|
|
container.Unmount()
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
container.LogEvent("copy")
|
|
|
|
return reader, nil
|
2013-07-17 00:07:41 -04:00
|
|
|
}
|
2013-10-04 22:25:15 -04:00
|
|
|
|
|
|
|
// Returns true if the container exposes a certain port
|
2014-02-11 19:48:44 -05:00
|
|
|
func (container *Container) Exposes(p nat.Port) bool {
|
2013-10-04 22:25:15 -04:00
|
|
|
_, exists := container.Config.ExposedPorts[p]
|
|
|
|
return exists
|
|
|
|
}
|
2013-11-29 10:40:44 -05:00
|
|
|
|
2014-03-07 21:42:29 -05:00
|
|
|
func (container *Container) HostConfig() *runconfig.HostConfig {
|
2015-05-08 22:06:53 -04:00
|
|
|
return container.hostConfig
|
2014-03-07 21:42:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) SetHostConfig(hostConfig *runconfig.HostConfig) {
|
|
|
|
container.hostConfig = hostConfig
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func (container *Container) getLogConfig() runconfig.LogConfig {
|
2015-02-04 14:04:58 -05:00
|
|
|
cfg := container.hostConfig.LogConfig
|
2015-04-09 00:23:30 -04:00
|
|
|
if cfg.Type != "" { // container has log driver configured
|
|
|
|
return cfg
|
2014-04-14 02:40:49 -04:00
|
|
|
}
|
2015-04-09 00:23:30 -04:00
|
|
|
// Use daemon's default log config for containers
|
|
|
|
return container.daemon.defaultLogConfig
|
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
func (container *Container) getLogger() (logger.Logger, error) {
|
|
|
|
cfg := container.getLogConfig()
|
|
|
|
c, err := logger.GetLogDriver(cfg.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get logging factory: %v", err)
|
|
|
|
}
|
|
|
|
ctx := logger.Context{
|
2015-05-29 17:00:46 -04:00
|
|
|
Config: cfg.Config,
|
|
|
|
ContainerID: container.ID,
|
|
|
|
ContainerName: container.Name,
|
|
|
|
ContainerEntrypoint: container.Path,
|
|
|
|
ContainerArgs: container.Args,
|
|
|
|
ContainerImageID: container.ImageID,
|
|
|
|
ContainerImageName: container.Config.Image,
|
|
|
|
ContainerCreated: container.Created,
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set logging file for "json-logger"
|
|
|
|
if cfg.Type == jsonfilelog.Name {
|
|
|
|
ctx.LogPath, err = container.GetRootResourcePath(fmt.Sprintf("%s-json.log", container.ID))
|
2015-04-20 14:26:39 -04:00
|
|
|
if err != nil {
|
2015-04-09 00:23:30 -04:00
|
|
|
return nil, err
|
2015-04-20 14:26:39 -04:00
|
|
|
}
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
return c(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) startLogging() error {
|
|
|
|
cfg := container.getLogConfig()
|
|
|
|
if cfg.Type == "none" {
|
|
|
|
return nil // do not start logging routines
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := container.getLogger()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to initialize logging driver: %v", err)
|
2014-04-14 02:40:49 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-03-18 14:44:14 -04:00
|
|
|
copier, err := logger.NewCopier(container.ID, map[string]io.Reader{"stdout": container.StdoutPipe(), "stderr": container.StderrPipe()}, l)
|
|
|
|
if err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-03-18 14:44:14 -04:00
|
|
|
container.logCopier = copier
|
|
|
|
copier.Run()
|
2015-02-04 14:04:58 -05:00
|
|
|
container.logDriver = l
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-04-09 00:23:30 -04:00
|
|
|
// set LogPath field only for json-file logdriver
|
|
|
|
if jl, ok := l.(*jsonfilelog.JSONFileLogger); ok {
|
|
|
|
container.LogPath = jl.LogPath()
|
|
|
|
}
|
|
|
|
|
2014-04-14 02:40:49 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) waitForStart() error {
|
2014-08-04 20:05:56 -04:00
|
|
|
container.monitor = newContainerMonitor(container, container.hostConfig.RestartPolicy)
|
2014-08-04 18:53:10 -04:00
|
|
|
|
2014-08-12 21:03:11 -04:00
|
|
|
// block until we either receive an error from the initial start of the container's
|
|
|
|
// process or until the process is running in the container
|
2014-04-14 02:40:49 -04:00
|
|
|
select {
|
2014-08-12 21:03:11 -04:00
|
|
|
case <-container.monitor.startSignal:
|
2014-09-30 02:16:27 -04:00
|
|
|
case err := <-promise.Go(container.monitor.Start):
|
2014-04-14 02:40:49 -04:00
|
|
|
return err
|
|
|
|
}
|
2014-08-12 21:03:11 -04:00
|
|
|
|
2014-04-14 02:40:49 -04:00
|
|
|
return nil
|
|
|
|
}
|
2014-04-14 07:23:43 -04:00
|
|
|
|
2014-04-29 04:08:19 -04:00
|
|
|
func (container *Container) GetProcessLabel() string {
|
|
|
|
// even if we have a process label return "" if we are running
|
|
|
|
// in privileged mode
|
|
|
|
if container.hostConfig.Privileged {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return container.ProcessLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) GetMountLabel() string {
|
|
|
|
if container.hostConfig.Privileged {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return container.MountLabel
|
|
|
|
}
|
2014-05-02 19:59:28 -04:00
|
|
|
|
2015-01-07 17:43:04 -05:00
|
|
|
func (container *Container) Stats() (*execdriver.ResourceStats, error) {
|
|
|
|
return container.daemon.Stats(container)
|
|
|
|
}
|
2015-02-05 19:24:47 -05:00
|
|
|
|
|
|
|
func (c *Container) LogDriverType() string {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
if c.hostConfig.LogConfig.Type == "" {
|
|
|
|
return c.daemon.defaultLogConfig.Type
|
|
|
|
}
|
|
|
|
return c.hostConfig.LogConfig.Type
|
|
|
|
}
|
2015-05-04 08:39:51 -04:00
|
|
|
|
|
|
|
func (container *Container) GetExecIDs() []string {
|
|
|
|
return container.execCommands.List()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) Exec(execConfig *execConfig) error {
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
waitStart := make(chan struct{})
|
|
|
|
|
|
|
|
callback := func(processConfig *execdriver.ProcessConfig, pid int) {
|
|
|
|
if processConfig.Tty {
|
|
|
|
// The callback is called after the process Start()
|
|
|
|
// so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
|
|
|
|
// which we close here.
|
|
|
|
if c, ok := processConfig.Stdout.(io.Closer); ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(waitStart)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use a callback here instead of a goroutine and an chan for
|
|
|
|
// syncronization purposes
|
|
|
|
cErr := promise.Go(func() error { return container.monitorExec(execConfig, callback) })
|
|
|
|
|
|
|
|
// Exec should not return until the process is actually running
|
|
|
|
select {
|
|
|
|
case <-waitStart:
|
|
|
|
case err := <-cErr:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) monitorExec(execConfig *execConfig, callback execdriver.StartCallback) error {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
exitCode int
|
|
|
|
)
|
|
|
|
|
|
|
|
pipes := execdriver.NewPipes(execConfig.StreamConfig.stdin, execConfig.StreamConfig.stdout, execConfig.StreamConfig.stderr, execConfig.OpenStdin)
|
|
|
|
exitCode, err = container.daemon.Exec(container, execConfig, pipes, callback)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error running command in existing container %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode)
|
|
|
|
if execConfig.OpenStdin {
|
|
|
|
if err := execConfig.StreamConfig.stdin.Close(); err != nil {
|
|
|
|
logrus.Errorf("Error closing stdin while running in %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := execConfig.StreamConfig.stdout.Clean(); err != nil {
|
|
|
|
logrus.Errorf("Error closing stdout while running in %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
if err := execConfig.StreamConfig.stderr.Clean(); err != nil {
|
|
|
|
logrus.Errorf("Error closing stderr while running in %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
if execConfig.ProcessConfig.Terminal != nil {
|
|
|
|
if err := execConfig.ProcessConfig.Terminal.Close(); err != nil {
|
|
|
|
logrus.Errorf("Error closing terminal while running in container %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2015-05-05 16:25:05 -04:00
|
|
|
|
|
|
|
func (c *Container) Attach(stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
|
|
|
|
return attach(&c.StreamConfig, c.Config.OpenStdin, c.Config.StdinOnce, c.Config.Tty, stdin, stdout, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) AttachWithLogs(stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool) error {
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2015-05-05 16:25:05 -04:00
|
|
|
if logs {
|
2015-04-09 00:23:30 -04:00
|
|
|
logDriver, err := c.getLogger()
|
|
|
|
cLog, err := logDriver.GetReader()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error reading logs: %s", err)
|
|
|
|
} else if c.LogDriverType() != jsonfilelog.Name {
|
|
|
|
logrus.Errorf("Reading logs not implemented for driver %s", c.LogDriverType())
|
2015-05-05 16:25:05 -04:00
|
|
|
} else {
|
|
|
|
dec := json.NewDecoder(cLog)
|
|
|
|
for {
|
|
|
|
l := &jsonlog.JSONLog{}
|
|
|
|
|
|
|
|
if err := dec.Decode(l); err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
logrus.Errorf("Error streaming logs: %s", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if l.Stream == "stdout" && stdout != nil {
|
|
|
|
io.WriteString(stdout, l.Log)
|
|
|
|
}
|
|
|
|
if l.Stream == "stderr" && stderr != nil {
|
|
|
|
io.WriteString(stderr, l.Log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 14:19:39 -04:00
|
|
|
c.LogEvent("attach")
|
|
|
|
|
2015-05-05 16:25:05 -04:00
|
|
|
//stream
|
|
|
|
if stream {
|
|
|
|
var stdinPipe io.ReadCloser
|
|
|
|
if stdin != nil {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer logrus.Debugf("Closing buffered stdin pipe")
|
|
|
|
io.Copy(w, stdin)
|
|
|
|
}()
|
|
|
|
stdinPipe = r
|
|
|
|
}
|
|
|
|
<-c.Attach(stdinPipe, stdout, stderr)
|
|
|
|
// If we are in stdinonce mode, wait for the process to end
|
|
|
|
// otherwise, simply return
|
|
|
|
if c.Config.StdinOnce && !c.Config.Tty {
|
|
|
|
c.WaitStop(-1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func attach(streamConfig *StreamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
|
|
|
|
var (
|
|
|
|
cStdout, cStderr io.ReadCloser
|
|
|
|
cStdin io.WriteCloser
|
|
|
|
wg sync.WaitGroup
|
|
|
|
errors = make(chan error, 3)
|
|
|
|
)
|
|
|
|
|
|
|
|
if stdin != nil && openStdin {
|
|
|
|
cStdin = streamConfig.StdinPipe()
|
|
|
|
wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stdout != nil {
|
|
|
|
cStdout = streamConfig.StdoutPipe()
|
|
|
|
wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stderr != nil {
|
|
|
|
cStderr = streamConfig.StderrPipe()
|
|
|
|
wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect stdin of container to the http conn.
|
|
|
|
go func() {
|
|
|
|
if stdin == nil || !openStdin {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.Debugf("attach: stdin: begin")
|
|
|
|
defer func() {
|
|
|
|
if stdinOnce && !tty {
|
|
|
|
cStdin.Close()
|
|
|
|
} else {
|
|
|
|
// No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
|
|
|
|
if cStdout != nil {
|
|
|
|
cStdout.Close()
|
|
|
|
}
|
|
|
|
if cStderr != nil {
|
|
|
|
cStderr.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
logrus.Debugf("attach: stdin: end")
|
|
|
|
}()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if tty {
|
|
|
|
_, err = copyEscapable(cStdin, stdin)
|
|
|
|
} else {
|
|
|
|
_, err = io.Copy(cStdin, stdin)
|
|
|
|
|
|
|
|
}
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("attach: stdin: %s", err)
|
|
|
|
errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) {
|
|
|
|
if stream == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// Make sure stdin gets closed
|
|
|
|
if stdin != nil {
|
|
|
|
stdin.Close()
|
|
|
|
}
|
|
|
|
streamPipe.Close()
|
|
|
|
wg.Done()
|
|
|
|
logrus.Debugf("attach: %s: end", name)
|
|
|
|
}()
|
|
|
|
|
|
|
|
logrus.Debugf("attach: %s: begin", name)
|
|
|
|
_, err := io.Copy(stream, streamPipe)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("attach: %s: %v", name, err)
|
|
|
|
errors <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go attachStream("stdout", stdout, cStdout)
|
|
|
|
go attachStream("stderr", stderr, cStderr)
|
|
|
|
|
|
|
|
return promise.Go(func() error {
|
|
|
|
wg.Wait()
|
|
|
|
close(errors)
|
|
|
|
for err := range errors {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code c/c from io.Copy() modified to handle escape sequence
|
|
|
|
func copyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) {
|
|
|
|
buf := make([]byte, 32*1024)
|
|
|
|
for {
|
|
|
|
nr, er := src.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
// ---- Docker addition
|
|
|
|
// char 16 is C-p
|
|
|
|
if nr == 1 && buf[0] == 16 {
|
|
|
|
nr, er = src.Read(buf)
|
|
|
|
// char 17 is C-q
|
|
|
|
if nr == 1 && buf[0] == 17 {
|
|
|
|
if err := src.Close(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ---- End of docker
|
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, err
|
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
|
|
|
|
func (container *Container) networkMounts() []execdriver.Mount {
|
|
|
|
var mounts []execdriver.Mount
|
|
|
|
if container.ResolvConfPath != "" {
|
2015-05-27 15:29:49 -04:00
|
|
|
label.SetFileLabel(container.ResolvConfPath, container.MountLabel)
|
2015-05-19 16:05:25 -04:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.ResolvConfPath,
|
|
|
|
Destination: "/etc/resolv.conf",
|
|
|
|
Writable: !container.hostConfig.ReadonlyRootfs,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if container.HostnamePath != "" {
|
2015-05-27 15:29:49 -04:00
|
|
|
label.SetFileLabel(container.HostnamePath, container.MountLabel)
|
2015-05-19 16:05:25 -04:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostnamePath,
|
|
|
|
Destination: "/etc/hostname",
|
|
|
|
Writable: !container.hostConfig.ReadonlyRootfs,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if container.HostsPath != "" {
|
2015-05-27 15:29:49 -04:00
|
|
|
label.SetFileLabel(container.HostsPath, container.MountLabel)
|
2015-05-19 16:05:25 -04:00
|
|
|
mounts = append(mounts, execdriver.Mount{
|
|
|
|
Source: container.HostsPath,
|
|
|
|
Destination: "/etc/hosts",
|
|
|
|
Writable: !container.hostConfig.ReadonlyRootfs,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return mounts
|
|
|
|
}
|
|
|
|
|
2015-05-28 16:44:55 -04:00
|
|
|
func (container *Container) addBindMountPoint(name, source, destination string, rw bool) {
|
|
|
|
container.MountPoints[destination] = &mountPoint{
|
|
|
|
Name: name,
|
|
|
|
Source: source,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:53:53 -04:00
|
|
|
func (container *Container) addLocalMountPoint(name, destination string, rw bool) {
|
2015-05-19 16:05:25 -04:00
|
|
|
container.MountPoints[destination] = &mountPoint{
|
|
|
|
Name: name,
|
|
|
|
Driver: volume.DefaultDriverName,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:53:53 -04:00
|
|
|
func (container *Container) addMountPointWithVolume(destination string, vol volume.Volume, rw bool) {
|
2015-05-19 16:05:25 -04:00
|
|
|
container.MountPoints[destination] = &mountPoint{
|
|
|
|
Name: vol.Name(),
|
|
|
|
Driver: vol.DriverName(),
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
Volume: vol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:53:53 -04:00
|
|
|
func (container *Container) isDestinationMounted(destination string) bool {
|
2015-05-19 16:05:25 -04:00
|
|
|
return container.MountPoints[destination] != nil
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:53:53 -04:00
|
|
|
func (container *Container) prepareMountPoints() error {
|
2015-05-19 16:05:25 -04:00
|
|
|
for _, config := range container.MountPoints {
|
|
|
|
if len(config.Driver) > 0 {
|
|
|
|
v, err := createVolume(config.Name, config.Driver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Volume = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:53:53 -04:00
|
|
|
func (container *Container) removeMountPoints() error {
|
2015-05-19 16:05:25 -04:00
|
|
|
for _, m := range container.MountPoints {
|
|
|
|
if m.Volume != nil {
|
|
|
|
if err := removeVolume(m.Volume); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:53:53 -04:00
|
|
|
func (container *Container) shouldRestart() bool {
|
2015-05-19 16:05:25 -04:00
|
|
|
return container.hostConfig.RestartPolicy.Name == "always" ||
|
|
|
|
(container.hostConfig.RestartPolicy.Name == "on-failure" && container.ExitCode != 0)
|
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
|
|
|
func (container *Container) copyImagePathContent(v volume.Volume, destination string) error {
|
|
|
|
rootfs, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, destination), container.basefs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = ioutil.ReadDir(rootfs); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := v.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := copyExistingContents(rootfs, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.Unmount()
|
|
|
|
}
|