Example #1
0
// 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,
		},
	})
}
Example #2
0
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
}
Example #3
0
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
}
Example #4
0
// 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)
}