Exemplo n.º 1
0
// EnsureItExists make sure the network exists and return an error if it does not exists
// and cannot be created.
func (n *Network) EnsureItExists(ctx context.Context) error {
	networkResource, err := n.Inspect(ctx)
	if n.external {
		if client.IsErrNetworkNotFound(err) {
			// FIXME(vdemeester) introduce some libcompose error type
			return fmt.Errorf("Network %s declared as external, but could not be found. Please create the network manually using docker network create %s and try again", n.fullName(), n.fullName())
		}
		return err
	}
	if err != nil && client.IsErrNetworkNotFound(err) {
		return n.create(ctx)
	}
	if n.driver != "" && networkResource.Driver != n.driver {
		return fmt.Errorf("Network %q needs to be recreated - driver has changed", n.fullName())
	}
	if len(n.driverOptions) != 0 && !reflect.DeepEqual(networkResource.Options, n.driverOptions) {
		return fmt.Errorf("Network %q needs to be recreated - options have changed", n.fullName())
	}
	return err
}
Exemplo n.º 2
0
// cleanUpDockerDandlingEndpoints cleans all endpoints that are dandling by checking out
// if a particular endpoint has its container running.
func (d *Daemon) cleanUpDockerDandlingEndpoints() {
	eps, _ := d.EndpointsGet()
	if eps == nil {
		return
	}

	cleanUp := func(ep endpoint.Endpoint) {
		log.Infof("Endpoint %d not found in docker, cleaning up...", ep.ID)
		d.EndpointLeave(ep.ID)
		// FIXME: IPV4
		if ep.IPv6 != nil {
			if ep.IsCNI() {
				d.ReleaseIP(ipam.CNIIPAMType, ep.IPv6.IPAMReq())
			} else if ep.IsLibnetwork() {
				d.ReleaseIP(ipam.LibnetworkIPAMType, ep.IPv6.IPAMReq())
			}
		}

	}

	for _, ep := range eps {
		log.Debugf("Checking if endpoint is running in docker %d", ep.ID)
		if ep.DockerNetworkID != "" {
			nls, err := d.dockerClient.NetworkInspect(ctx.Background(), ep.DockerNetworkID)
			if dockerAPI.IsErrNetworkNotFound(err) {
				cleanUp(ep)
				continue
			}
			if err != nil {
				continue
			}
			found := false
			for _, v := range nls.Containers {
				if v.EndpointID == ep.DockerEndpointID {
					found = true
					break
				}
			}
			if !found {
				cleanUp(ep)
				continue
			}
		} else if ep.DockerID != "" {
			cont, err := d.dockerClient.ContainerInspect(ctx.Background(), ep.DockerID)
			if dockerAPI.IsErrContainerNotFound(err) {
				cleanUp(ep)
				continue
			}
			if err != nil {
				continue
			}
			if !cont.State.Running {
				cleanUp(ep)
				continue
			}
		} else {
			cleanUp(ep)
			continue
		}
	}
}