Example #1
0
func (handler *ContainersHandlersImpl) RemoveContainerHandler(params containers.ContainerRemoveParams) middleware.Responder {
	defer trace.End(trace.Begin(params.ID))

	// get the indicated container for removal
	cID := uid.Parse(params.ID)
	h := exec.GetContainer(context.Background(), cID)
	if h == nil || h.ExecConfig == nil {
		return containers.NewContainerRemoveNotFound()
	}

	container := exec.Containers.Container(h.ExecConfig.ID)
	if container == nil {
		return containers.NewGetStateNotFound()
	}

	// NOTE: this should allowing batching of operations, as with Create, Start, Stop, et al
	err := container.Remove(context.Background(), handler.handlerCtx.Session)
	if err != nil {
		switch err := err.(type) {
		case exec.NotFoundError:
			return containers.NewContainerRemoveNotFound()
		case exec.RemovePowerError:
			return containers.NewContainerRemoveConflict().WithPayload(&models.Error{Message: err.Error()})
		default:
			return containers.NewContainerRemoveInternalServerError()
		}
	}

	return containers.NewContainerRemoveOK()
}
Example #2
0
func (c *Container) Refresh(ctx context.Context) error {
	c.Lock()
	defer c.Unlock()

	// this will "refresh" the container executor config that contains
	// the current ip addresses
	h := exec.GetContainer(ctx, c.ID())
	if h == nil {
		return fmt.Errorf("could not find container %s", c.ID())
	}
	defer h.Close()

	for _, e := range c.endpoints {
		s := e.Scope()
		if !s.isDynamic() {
			continue
		}

		ne := h.ExecConfig.Networks[s.Name()]
		if ne == nil {
			return fmt.Errorf("container config does not have info for network scope %s", s.Name())
		}

		e.ip = ne.Assigned.IP
		if err := s.Refresh(h); err != nil {
			return err
		}
	}

	return nil
}
Example #3
0
func (handler *ContainersHandlersImpl) GetHandler(params containers.GetParams) middleware.Responder {
	defer trace.End(trace.Begin(params.ID))

	h := exec.GetContainer(context.Background(), uid.Parse(params.ID))
	if h == nil {
		return containers.NewGetNotFound().WithPayload(&models.Error{Message: fmt.Sprintf("container %s not found", params.ID)})
	}

	return containers.NewGetOK().WithPayload(h.String())
}
Example #4
0
func (handler *ScopesHandlersImpl) listScopes(idName string) ([]*models.ScopeConfig, error) {
	_scopes, err := handler.netCtx.Scopes(&idName)
	if err != nil {
		return nil, err
	}

	cfgs := make([]*models.ScopeConfig, len(_scopes))
	updated := make(map[uid.UID]*exec.Handle)
	for i, s := range _scopes {
		for _, e := range s.Endpoints() {
			// update the container config, if necessary
			// do not need do this for non-bridge scopes, since
			// IPAM is done by the port layer. For other
			// scopes types, like external, the network
			// may be using DHCP, in which case we need to
			// get the current IP address, and other network
			// info from the container VM.
			if s.Type() != network.BridgeScopeType {
				var h *exec.Handle
				c := e.Container().ID()
				if h = updated[c]; h == nil {
					h = exec.GetContainer(c)
					if _, err := h.Update(context.Background(), handler.handlerCtx.Session); err != nil {
						return nil, err
					}

					updated[c] = h
				}

				if err = handler.netCtx.UpdateContainer(h); err != nil {
					return nil, err
				}
			}
		}

		cfgs[i] = toScopeConfig(s)
	}

	for _, h := range updated {
		h.Close()
	}

	return cfgs, nil
}
Example #5
0
// handleEvent processes events
func handleEvent(netctx *Context, ie events.Event) {
	switch ie.String() {
	case events.ContainerPoweredOff:
		handle := exec.GetContainer(context.Background(), uid.Parse(ie.Reference()))
		if handle == nil {
			log.Errorf("Container %s not found - unable to UnbindContainer", ie.Reference())
			return
		}
		defer handle.Close()
		if _, err := netctx.UnbindContainer(handle); err != nil {
			log.Warnf("Failed to unbind container %s: %s", ie.Reference(), err)
			return
		}

		if err := handle.Commit(context.Background(), nil, nil); err != nil {
			log.Warnf("Failed to commit handle after network unbind for container %s: %s", ie.Reference(), err)
		}

	}
	return
}