func (c *Container) Name() string {
	return c.name
}
Name returns the container name.
package docker
import (
	"fmt"
	"io"
	"math"
	"os"
	"strings"
	"golang.org/x/net/context"
	"github.com/Sirupsen/logrus"
	"github.com/docker/docker/pkg/promise"
	"github.com/docker/docker/pkg/stdcopy"
	"github.com/docker/docker/pkg/term"
	"github.com/docker/engine-api/client"
	"github.com/docker/engine-api/types"
	"github.com/docker/engine-api/types/container"
	"github.com/docker/go-connections/nat"
	"github.com/docker/libcompose/config"
	"github.com/docker/libcompose/labels"
	"github.com/docker/libcompose/logger"
	"github.com/docker/libcompose/project"
	"github.com/docker/libcompose/project/events"
	"github.com/docker/engine-api/types/network"
	util "github.com/docker/libcompose/utils"
)
type Container struct {
	name            string
	serviceName     string
	projectName     string
	containerNumber int
	oneOff          bool
	eventNotifier   events.Notifier
	loggerFactory   logger.Factory
	client          client.APIClient
	service *Service
}
func NewContainer(client client.APIClient, name string, containerNumber int, service *Service) *Container {
	return &Container{
		client:          client,
		name:            name,
		containerNumber: containerNumber,
		serviceName:   service.name,
		projectName:   service.context.Project.Name,
		eventNotifier: service.context.Project,
		loggerFactory: service.context.LoggerFactory,
		service: service,
	}
}
func NewOneOffContainer(client client.APIClient, name string, containerNumber int, service *Service) *Container {
	c := NewContainer(client, name, containerNumber, service)
	c.oneOff = true
	return c
}
func (c *Container) findExisting(ctx context.Context) (*types.ContainerJSON, error) {
	return GetContainer(ctx, c.client, c.name)
}
func (c *Container) Info(ctx context.Context, qFlag bool) (project.Info, error) {
	container, err := c.findExisting(ctx)
	if err != nil || container == nil {
		return nil, err
	}
	infos, err := GetContainersByFilter(ctx, c.client, map[string][]string{
		"name": {container.Name},
	})
	if err != nil || len(infos) == 0 {
		return nil, err
	}
	info := infos[0]
	result := project.Info{}
	if qFlag {
		result = append(result, project.InfoPart{Key: "Id", Value: container.ID})
	} else {
		result = append(result, project.InfoPart{Key: "Name", Value: name(info.Names)})
		result = append(result, project.InfoPart{Key: "Command", Value: info.Command})
		result = append(result, project.InfoPart{Key: "State", Value: info.Status})
		result = append(result, project.InfoPart{Key: "Ports", Value: portString(info.Ports)})
	}
	return result, nil
}
func portString(ports []types.Port) string {
	result := []string{}
	for _, port := range ports {
		if port.PublicPort > 0 {
			result = append(result, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
		} else {
			result = append(result, fmt.Sprintf("%d/%s", port.PrivatePort, port.Type))
		}
	}
	return strings.Join(result, ", ")
}
func name(names []string) string {
	max := math.MaxInt32
	var current string
	for _, v := range names {
		if len(v) < max {
			max = len(v)
			current = v
		}
	}
	return current[1:]
}
func (c *Container) Recreate(ctx context.Context, imageName string) (*types.ContainerJSON, error) {
	container, err := c.findExisting(ctx)
	if err != nil || container == nil {
		return nil, err
	}
	hash := container.Config.Labels[labels.HASH.Str()]
	legacyHash := container.Config.Labels[labels.HASH_LEGACY.Str()]
	if hash == "" && legacyHash != "" {
		hash = legacyHash
	}
	if hash == "" {
		return nil, fmt.Errorf("Failed to find hash on old container: %s", container.Name)
	}
	name := container.Name[1:]
	newName := fmt.Sprintf("%s_%s", name, container.ID[:12])
	logrus.Debugf("Renaming %s => %s", name, newName)
	if err := c.client.ContainerRename(ctx, container.ID, newName); err != nil {
		logrus.Errorf("Failed to rename old container %s", c.name)
		return nil, err
	}
	newContainer, err := c.createContainer(ctx, imageName, container.ID, nil)
	if err != nil {
		return nil, err
	}
	logrus.Debugf("Created replacement container %s", newContainer.ID)
	if err := c.client.ContainerRemove(ctx, types.ContainerRemoveOptions{
		ContainerID:   container.ID,
		Force:         true,
		RemoveVolumes: false,
	}); err != nil {
		logrus.Errorf("Failed to remove old container %s", c.name)
		return nil, err
	}
	logrus.Debugf("Removed old container %s %s", c.name, container.ID)
	return newContainer, nil
}
func (c *Container) Create(ctx context.Context, imageName string) (*types.ContainerJSON, error) {
	return c.CreateWithOverride(ctx, imageName, nil)
}
func (c *Container) CreateWithOverride(ctx context.Context, imageName string, configOverride *config.ServiceConfig) (*types.ContainerJSON, error) {
	container, err := c.findExisting(ctx)
	if err != nil {
		return nil, err
	}
	if container == nil {
		container, err = c.createContainer(ctx, imageName, "", configOverride)
		if err != nil {
			return nil, err
		}
		c.eventNotifier.Notify(events.ContainerCreated, c.serviceName, map[string]string{
			"name": c.Name(),
		})
	}
	return container, err
}
func (c *Container) Stop(ctx context.Context, timeout int) error {
	return c.withContainer(ctx, func(container *types.ContainerJSON) error {
		return c.client.ContainerStop(ctx, container.ID, timeout)
	})
}
func (c *Container) Pause(ctx context.Context) error {
	return c.withContainer(ctx, func(container *types.ContainerJSON) error {
		if !container.State.Paused {
			return c.client.ContainerPause(ctx, container.ID)
		}
		return nil
	})
}
func (c *Container) Unpause(ctx context.Context) error {
	return c.withContainer(ctx, func(container *types.ContainerJSON) error {
		if container.State.Paused {
			return c.client.ContainerUnpause(ctx, container.ID)
		}
		return nil
	})
}
func (c *Container) Kill(ctx context.Context, signal string) error {
	return c.withContainer(ctx, func(container *types.ContainerJSON) error {
		return c.client.ContainerKill(ctx, container.ID, signal)
	})
}
func (c *Container) Delete(ctx context.Context, removeVolume bool) error {
	container, err := c.findExisting(ctx)
	if err != nil || container == nil {
		return err
	}
	info, err := c.client.ContainerInspect(ctx, container.ID)
	if err != nil {
		return err
	}
	if !info.State.Running {
		return c.client.ContainerRemove(ctx, types.ContainerRemoveOptions{
			ContainerID:   container.ID,
			Force:         true,
			RemoveVolumes: removeVolume,
		})
	}
	return nil
}
func (c *Container) IsRunning(ctx context.Context) (bool, error) {
	container, err := c.findExisting(ctx)
	if err != nil || container == nil {
		return false, err
	}
	info, err := c.client.ContainerInspect(ctx, container.ID)
	if err != nil {
		return false, err
	}
	return info.State.Running, nil
}
func (c *Container) Run(ctx context.Context, imageName string, configOverride *config.ServiceConfig) (int, error) {
	var (
		errCh       chan error
		out, stderr io.Writer
		in          io.ReadCloser
	)
	container, err := c.createContainer(ctx, imageName, "", configOverride)
	if err != nil {
		return -1, err
	}
	if configOverride.StdinOpen {
		in = os.Stdin
	}
	if configOverride.Tty {
		out = os.Stdout
	}
	if configOverride.Tty {
		stderr = os.Stderr
	}
	options := types.ContainerAttachOptions{
		ContainerID: container.ID,
		Stream:      true,
		Stdin:       configOverride.StdinOpen,
		Stdout:      configOverride.Tty,
		Stderr:      configOverride.Tty,
	}
	resp, err := c.client.ContainerAttach(ctx, options)
	if err != nil {
		return -1, err
	}
	inFd, _ := term.GetFdInfo(in)
	state, err := term.SetRawTerminal(inFd)
	if err != nil {
		return -1, err
	}
	defer term.RestoreTerminal(inFd, state)
	errCh = promise.Go(func() error {
		return holdHijackedConnection(configOverride.Tty, in, out, stderr, resp)
	})
	if err := c.client.ContainerStart(ctx, container.ID); err != nil {
		return -1, err
	}
	if err := <-errCh; err != nil {
		logrus.Debugf("Error hijack: %s", err)
		return -1, err
	}
	exitedContainer, err := c.client.ContainerInspect(ctx, container.ID)
	if err != nil {
		return -1, err
	}
	return exitedContainer.State.ExitCode, nil
}
func holdHijackedConnection(tty bool, inputStream io.ReadCloser, outputStream, errorStream io.Writer, resp types.HijackedResponse) error {
	var err error
	receiveStdout := make(chan error, 1)
	if outputStream != nil || errorStream != nil {
		go func() {
			if tty && outputStream != nil {
				_, err = io.Copy(outputStream, resp.Reader)
			} else {
				_, err = stdcopy.StdCopy(outputStream, errorStream, resp.Reader)
			}
			logrus.Debugf("[hijack] End of stdout")
			receiveStdout <- err
		}()
	}
	stdinDone := make(chan struct{})
	go func() {
		if inputStream != nil {
			io.Copy(resp.Conn, inputStream)
			logrus.Debugf("[hijack] End of stdin")
		}
		if err := resp.CloseWrite(); err != nil {
			logrus.Debugf("Couldn't send EOF: %s", err)
		}
		close(stdinDone)
	}()
	select {
	case err := <-receiveStdout:
		if err != nil {
			logrus.Debugf("Error receiveStdout: %s", err)
			return err
		}
	case <-stdinDone:
		if outputStream != nil || errorStream != nil {
			if err := <-receiveStdout; err != nil {
				logrus.Debugf("Error receiveStdout: %s", err)
				return err
			}
		}
	}
	return nil
}
func (c *Container) Up(ctx context.Context, imageName string) error {
	var err error
	container, err := c.Create(ctx, imageName)
	if err != nil {
		return err
	}
	if !container.State.Running {
		c.Start(container)
	}
	return nil
}
func (c *Container) Start(container *types.ContainerJSON) error {
	logrus.WithFields(logrus.Fields{"container.ID": container.ID, "c.name": c.name}).Debug("Starting container")
	if err := c.client.ContainerStart(context.Background(), container.ID); err != nil {
		logrus.WithFields(logrus.Fields{"container.ID": container.ID, "c.name": c.name}).Debug("Failed to start container")
		return err
	}
	c.eventNotifier.Notify(events.ContainerStarted, c.serviceName, map[string]string{
		"name": c.Name(),
	})
	return nil
}
func (c *Container) OutOfSync(ctx context.Context, imageName string) (bool, error) {
	container, err := c.findExisting(ctx)
	if err != nil || container == nil {
		return false, err
	}
	if container.Config.Image != imageName {
		logrus.Debugf("Images for %s do not match %s!=%s", c.name, container.Config.Image, imageName)
		return true, nil
	}
	if container.Config.Labels[labels.HASH.Str()] != c.getHash() {
		logrus.Debugf("Hashes for %s do not match %s!=%s", c.name, container.Config.Labels[labels.HASH.Str()], c.getHash())
		return true, nil
	}
	image, _, err := c.client.ImageInspectWithRaw(ctx, container.Config.Image, false)
	if err != nil {
		if client.IsErrImageNotFound(err) {
			logrus.Debugf("Image %s do not exist, do not know if it's out of sync", container.Config.Image)
			return false, nil
		}
		return false, err
	}
	logrus.Debugf("Checking existing image name vs id: %s == %s", image.ID, container.Image)
	return image.ID != container.Image, err
}
func (c *Container) getHash() string {
	return config.GetServiceHash(c.serviceName, c.service.Config())
}
func volumeBinds(volumes map[string]struct{}, container *types.ContainerJSON) []string {
	result := make([]string, 0, len(container.Mounts))
	for _, mount := range container.Mounts {
		if _, ok := volumes[mount.Destination]; ok {
			result = append(result, fmt.Sprint(mount.Source, ":", mount.Destination))
		}
	}
	return result
}
func (c *Container) createContainer(ctx context.Context, imageName, oldContainer string, configOverride *config.ServiceConfig) (*types.ContainerJSON, error) {
	serviceConfig := c.service.serviceConfig
	if configOverride != nil {
		serviceConfig.Command = configOverride.Command
		serviceConfig.Tty = configOverride.Tty
		serviceConfig.StdinOpen = configOverride.StdinOpen
	}
	configWrapper, err := ConvertToAPI(c.service)
	if err != nil {
		return nil, err
	}
	configWrapper.Config.Image = imageName
	if configWrapper.Config.Labels == nil {
		configWrapper.Config.Labels = map[string]string{}
	}
	oneOffString := "False"
	if c.oneOff {
		oneOffString = "True"
	}
	configWrapper.Config.Labels[labels.SERVICE.Str()] = c.serviceName
	configWrapper.Config.Labels[labels.PROJECT.Str()] = c.projectName
	configWrapper.Config.Labels[labels.HASH.Str()] = c.getHash()
	configWrapper.Config.Labels[labels.ONEOFF.Str()] = oneOffString
	configWrapper.Config.Labels[labels.NUMBER.Str()] = fmt.Sprint(c.containerNumber)
	configWrapper.Config.Labels[labels.VERSION.Str()] = ComposeVersion
	err = c.populateAdditionalHostConfig(configWrapper.HostConfig)
	if err != nil {
		return nil, err
	}
	if oldContainer != "" {
		info, err := c.client.ContainerInspect(ctx, oldContainer)
		if err != nil {
			return nil, err
		}
		configWrapper.HostConfig.Binds = util.Merge(configWrapper.HostConfig.Binds, volumeBinds(configWrapper.Config.Volumes, &info))
	}
	networkConfig := configWrapper.NetworkingConfig
	if configWrapper.HostConfig.NetworkMode != "" && configWrapper.HostConfig.NetworkMode.IsUserDefined() {
		if networkConfig == nil {
			if _, ok := configWrapper.Config.Labels["io.rancher.user_docker.fix_ip"]; ok {
				networkConfig = &network.NetworkingConfig{
					EndpointsConfig: map[string]*network.EndpointSettings{
						string(configWrapper.HostConfig.NetworkMode): {
							IPAddress: configWrapper.Config.Labels["io.rancher.user_docker.fix_ip"],
							IPAMConfig: &network.EndpointIPAMConfig{
								IPv4Address: configWrapper.Config.Labels["io.rancher.user_docker.fix_ip"],
							},
						},
					},
				}
			} else {
				networkConfig = &network.NetworkingConfig{
					EndpointsConfig: map[string]*network.EndpointSettings{
						string(configWrapper.HostConfig.NetworkMode): {},
					},
				}
			}
		}
		for key, value := range networkConfig.EndpointsConfig {
			conf := value
			if value.Aliases == nil {
				value.Aliases = []string{}
			}
			value.Aliases = append(value.Aliases, c.serviceName)
			networkConfig.EndpointsConfig[key] = conf
		}
	}
	logrus.Debugf("Creating container %s %#v", c.name, configWrapper)
	container, err := c.client.ContainerCreate(ctx, configWrapper.Config, configWrapper.HostConfig, networkConfig, c.name)
	if err != nil {
		logrus.Debugf("Failed to create container %s: %v", c.name, err)
		return nil, err
	}
	return GetContainer(ctx, c.client, container.ID)
}
func (c *Container) populateAdditionalHostConfig(hostConfig *container.HostConfig) error {
	links := map[string]string{}
	for _, link := range c.service.DependentServices() {
		if !c.service.context.Project.ServiceConfigs.Has(link.Target) {
			continue
		}
		service, err := c.service.context.Project.CreateService(link.Target)
		if err != nil {
			return err
		}
		containers, err := service.Containers(context.Background())
		if err != nil {
			return err
		}
		if link.Type == project.RelTypeLink {
			c.addLinks(links, service, link, containers)
		} else if link.Type == project.RelTypeIpcNamespace {
			hostConfig, err = c.addIpc(hostConfig, service, containers)
		} else if link.Type == project.RelTypeNetNamespace {
			hostConfig, err = c.addNetNs(hostConfig, service, containers)
		}
		if err != nil {
			return err
		}
	}
	hostConfig.Links = []string{}
	for k, v := range links {
		hostConfig.Links = append(hostConfig.Links, strings.Join([]string{v, k}, ":"))
	}
	for _, v := range c.service.Config().ExternalLinks {
		hostConfig.Links = append(hostConfig.Links, v)
	}
	return nil
}
func (c *Container) addLinks(links map[string]string, service project.Service, rel project.ServiceRelationship, containers []project.Container) {
	for _, container := range containers {
		if _, ok := links[rel.Alias]; !ok {
			links[rel.Alias] = container.Name()
		}
		links[container.Name()] = container.Name()
	}
}
func (c *Container) addIpc(config *container.HostConfig, service project.Service, containers []project.Container) (*container.HostConfig, error) {
	if len(containers) == 0 {
		return nil, fmt.Errorf("Failed to find container for IPC %v", c.service.Config().Ipc)
	}
	id, err := containers[0].ID()
	if err != nil {
		return nil, err
	}
	config.IpcMode = container.IpcMode("container:" + id)
	return config, nil
}
func (c *Container) addNetNs(config *container.HostConfig, service project.Service, containers []project.Container) (*container.HostConfig, error) {
	if len(containers) == 0 {
		return nil, fmt.Errorf("Failed to find container for networks ns %v", c.service.Config().NetworkMode)
	}
	id, err := containers[0].ID()
	if err != nil {
		return nil, err
	}
	config.NetworkMode = container.NetworkMode("container:" + id)
	return config, nil
}
func (c *Container) ID() (string, error) {
	container, err := c.findExisting(context.Background())
	if container == nil {
		return "", err
	}
	return container.ID, err
}