// NetworkConnect connects the container to the specified network // FIXME(vdemeester) will be refactor with Container refactoring func (s *Service) NetworkConnect(ctx context.Context, c *container.Container, net *yaml.Network, oneOff bool) error { containerID, _ := c.ID() client := s.clientFactory.Create(s) internalLinks, err := s.getLinks() if err != nil { return err } links := []string{} // TODO(vdemeester) handle link to self (?) for k, v := range internalLinks { links = append(links, strings.Join([]string{v, k}, ":")) } for _, v := range s.serviceConfig.ExternalLinks { links = append(links, v) } aliases := []string{} if !oneOff { aliases = []string{s.Name()} } aliases = append(aliases, net.Aliases...) return client.NetworkConnect(ctx, net.RealName, containerID, &network.EndpointSettings{ Aliases: aliases, Links: links, IPAddress: net.IPv4Address, IPAMConfig: &network.EndpointIPAMConfig{ IPv4Address: net.IPv4Address, IPv6Address: net.IPv6Address, }, }) }
func (s *Service) connectContainerToNetworks(ctx context.Context, c *container.Container, oneOff bool) error { connectedNetworks, err := c.Networks() if err != nil { return nil } if s.serviceConfig.Networks != nil { for _, network := range s.serviceConfig.Networks.Networks { existingNetwork, ok := connectedNetworks[network.Name] if ok { // FIXME(vdemeester) implement alias checking (to not disconnect/reconnect for nothing) aliasPresent := false for _, alias := range existingNetwork.Aliases { // FIXME(vdemeester) use shortID instead of ID ID, _ := c.ID() if alias == ID { aliasPresent = true } } if aliasPresent { continue } if err := s.NetworkDisconnect(ctx, c, network, oneOff); err != nil { return err } } if err := s.NetworkConnect(ctx, c, network, oneOff); err != nil { return err } } } return 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.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) 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) return true, nil } image, err := image.InspectImage(ctx, s.clientFactory.Create(s), c.ImageConfig()) if err != nil { if client.IsErrImageNotFound(err) { logrus.Debugf("Image %s do not exist, do not know if it's out of sync", c.Image()) return false, nil } return false, err } logrus.Debugf("Checking existing image name vs id: %s == %s", image.ID, c.Image()) return image.ID != c.Image(), err }
func (s *Service) recreate(ctx context.Context, c *container.Container) (*container.Container, error) { name := c.Name() id, _ := c.ID() newName := fmt.Sprintf("%s_%s", name, 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()) return nil, err } namer := NewSingleNamer(name) newContainer, err := s.createContainer(ctx, namer, id, nil, false) if err != nil { return nil, err } newID, _ := newContainer.ID() logrus.Debugf("Created replacement container %s", newID) if err := c.Remove(ctx, 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(), id) return newContainer, nil }
// NetworkDisconnect disconnects the container from the specified network func (s *Service) NetworkDisconnect(ctx context.Context, c *container.Container, net *yaml.Network, oneOff bool) error { containerID, _ := c.ID() client := s.clientFactory.Create(s) return client.NetworkDisconnect(ctx, net.RealName, containerID, true) }