Skip to content

Commit

Permalink
Merge pull request docker#331 from vdemeester/262-fix
Browse files Browse the repository at this point in the history
Remove Container.name field…
  • Loading branch information
joshwget authored Jul 13, 2016
2 parents bc76e8b + fafa3d8 commit a12288b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
15 changes: 6 additions & 9 deletions docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
type Container struct {
// FIXME(vdemeester) Replace with ContainerClient with engine-api vendor update
client client.APIClient
name string
id string
container *types.ContainerJSON
}
Expand All @@ -40,18 +39,17 @@ func CreateContainer(ctx context.Context, client client.APIClient, name string,
if err != nil {
return nil, err
}
return New(ctx, client, container.ID, name)
return New(ctx, client, container.ID)
}

// New creates a container struct with the specified client, id and name
func New(ctx context.Context, client client.APIClient, id, name string) (*Container, error) {
func New(ctx context.Context, client client.APIClient, id string) (*Container, error) {
container, err := GetContainer(ctx, client, id)
if err != nil {
return nil, err
}
return &Container{
client: client,
name: name,
id: id,
container: container,
}, nil
Expand All @@ -61,7 +59,6 @@ func New(ctx context.Context, client client.APIClient, id, name string) (*Contai
func NewInspected(client client.APIClient, container *types.ContainerJSON) *Container {
return &Container{
client: client,
name: container.Name,
id: container.ID,
container: container,
}
Expand Down Expand Up @@ -290,9 +287,9 @@ func holdHijackedConnection(tty bool, inputStream io.ReadCloser, outputStream, e

// Start the specified container with the specified host config
func (c *Container) Start(ctx context.Context) error {
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "c.name": c.name}).Debug("Starting container")
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "container.Name": c.container.Name}).Debug("Starting container")
if err := c.client.ContainerStart(ctx, c.container.ID, types.ContainerStartOptions{}); err != nil {
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "c.name": c.name}).Debug("Failed to start container")
logrus.WithFields(logrus.Fields{"container.ID": c.container.ID, "container.Name": c.container.Name}).Debug("Failed to start container")
return err
}
return nil
Expand All @@ -305,7 +302,7 @@ func (c *Container) ID() (string, error) {

// Name returns the container name.
func (c *Container) Name() string {
return c.name
return c.container.Name
}

// Restart restarts the container if existing, does nothing otherwise.
Expand All @@ -327,7 +324,7 @@ func (c *Container) Log(ctx context.Context, l logger.Logger, follow bool) error
Follow: follow,
Tail: "all",
}
responseBody, err := c.client.ContainerLogs(ctx, c.name, options)
responseBody, err := c.client.ContainerLogs(ctx, c.container.ID, options)
if err != nil {
return err
}
Expand Down
16 changes: 7 additions & 9 deletions docker/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ func (s *Service) collectContainers(ctx context.Context) ([]*Container, error) {
result := []*Container{}

for _, container := range containers {
// Compose add "/" before name, so Name[1] will store actaul name.
name := strings.SplitAfter(container.Names[0], "/")
c, err := New(ctx, client, container.ID, name[1])
c, err := New(ctx, client, container.ID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -461,11 +459,11 @@ func (s *Service) recreateIfNeeded(ctx context.Context, c *Container, noRecreate
}

func (s *Service) recreate(ctx context.Context, c *Container) (*Container, error) {
name := c.name
name := c.Name()
newName := fmt.Sprintf("%s_%s", name, c.container.ID[:12])
logrus.Debugf("Renaming %s => %s", name, newName)
if err := c.Rename(ctx, newName); err != nil {
logrus.Errorf("Failed to rename old container %s", c.name)
logrus.Errorf("Failed to rename old container %s", c.Name())
return nil, err
}
namer := NewSingleNamer(name)
Expand All @@ -475,24 +473,24 @@ func (s *Service) recreate(ctx context.Context, c *Container) (*Container, error
}
logrus.Debugf("Created replacement container %s", newContainer.container.ID)
if err := c.Remove(ctx, false); err != nil {
logrus.Errorf("Failed to remove old container %s", c.name)
logrus.Errorf("Failed to remove old container %s", c.Name())
return nil, err
}
logrus.Debugf("Removed old container %s %s", c.name, c.container.ID)
logrus.Debugf("Removed old container %s %s", c.Name(), c.container.ID)
return newContainer, nil
}

// OutOfSync checks if the container is out of sync with the service definition.
// It looks if the the service hash container label is the same as the computed one.
func (s *Service) OutOfSync(ctx context.Context, c *Container) (bool, error) {
if c.ImageConfig() != s.serviceConfig.Image {
logrus.Debugf("Images for %s do not match %s!=%s", c.name, c.ImageConfig(), s.serviceConfig.Image)
logrus.Debugf("Images for %s do not match %s!=%s", c.Name(), c.ImageConfig(), s.serviceConfig.Image)
return true, nil
}

expectedHash := config.GetServiceHash(s.name, s.Config())
if c.Hash() != expectedHash {
logrus.Debugf("Hashes for %s do not match %s!=%s", c.name, c.Hash(), expectedHash)
logrus.Debugf("Hashes for %s do not match %s!=%s", c.Name(), c.Hash(), expectedHash)
return true, nil
}

Expand Down

0 comments on commit a12288b

Please sign in to comment.