Esempio n. 1
0
func (handler *ScopesHandlersImpl) ScopesAddContainer(params scopes.AddContainerParams) middleware.Responder {
	defer trace.End(trace.Begin("ScopesAddContainer"))

	h := exec.GetHandle(params.Config.Handle)
	if h == nil {
		return scopes.NewAddContainerNotFound().WithPayload(&models.Error{Message: "container not found"})
	}

	err := func() error {
		var ip *net.IP
		if params.Config.NetworkConfig.Address != nil {
			i := net.ParseIP(*params.Config.NetworkConfig.Address)
			if i == nil {
				return fmt.Errorf("invalid ip address")
			}

			ip = &i
		}

		return handler.netCtx.AddContainer(h, params.Config.NetworkConfig.NetworkName, ip)
	}()

	if err != nil {
		if _, ok := err.(*network.ResourceNotFoundError); ok {
			return scopes.NewAddContainerNotFound().WithPayload(errorPayload(err))
		}

		return scopes.NewAddContainerInternalServerError().WithPayload(errorPayload(err))
	}

	return scopes.NewAddContainerOK().WithPayload(h.String())
}
Esempio n. 2
0
func (handler *ContainersHandlersImpl) GetStateHandler(params containers.GetStateParams) middleware.Responder {
	defer trace.End(trace.Begin(fmt.Sprintf("handle(%s)", params.Handle)))

	// NOTE: I've no idea why GetStateHandler takes a handle instead of an ID - hopefully there was a reason for an inspection
	// operation to take this path
	h := exec.GetHandle(params.Handle)
	if h == nil || h.ExecConfig == nil {
		return containers.NewGetStateNotFound()
	}

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

	var state string
	switch container.CurrentState() {
	case exec.StateRunning:
		state = "RUNNING"

	case exec.StateStopped:
		state = "STOPPED"

	case exec.StateCreated:
		state = "CREATED"

	default:
		return containers.NewGetStateDefault(http.StatusServiceUnavailable)
	}

	return containers.NewGetStateOK().WithPayload(&models.ContainerGetStateResponse{Handle: h.String(), State: state})
}
Esempio n. 3
0
func (handler *ScopesHandlersImpl) ScopesUnbindContainer(params scopes.UnbindContainerParams) middleware.Responder {
	defer trace.End(trace.Begin(fmt.Sprintf("handle(%s)", params.Handle)))

	h := exec.GetHandle(params.Handle)
	if h == nil {
		return scopes.NewUnbindContainerNotFound()
	}

	var endpoints []*network.Endpoint
	var err error
	if endpoints, err = handler.netCtx.UnbindContainer(h); err != nil {
		switch err := err.(type) {
		case network.ResourceNotFoundError:
			return scopes.NewUnbindContainerNotFound().WithPayload(errorPayload(err))

		default:
			return scopes.NewUnbindContainerInternalServerError().WithPayload(errorPayload(err))
		}
	}

	res := &models.UnbindContainerResponse{
		Handle:    h.String(),
		Endpoints: make([]*models.EndpointConfig, len(endpoints)),
	}
	for i, e := range endpoints {
		res.Endpoints[i] = toEndpointConfig(e)
	}

	return scopes.NewUnbindContainerOK().WithPayload(res)
}
Esempio n. 4
0
//VolumeJoin : modifies the config spec of a container to mount the specified container
func (handler *StorageHandlersImpl) VolumeJoin(params storage.VolumeJoinParams) middleware.Responder {
	defer trace.End(trace.Begin("storage_handlers.RemoveVolume"))
	actualHandle := epl.GetHandle(params.JoinArgs.Handle)

	//Note: Name should already be populated by now.
	volume, err := storageVolumeLayer.VolumeGet(context.Background(), params.Name)
	if err != nil {
		log.Errorf("Volumes: StorageHandler : %#v", err)
		return storage.NewVolumeJoinInternalServerError().WithPayload(&models.Error{
			Code:    swag.Int64(http.StatusInternalServerError),
			Message: err.Error(),
		})
	}
	log.Infof("found volume %s for volume join", volume.ID)
	actualHandle, err = vsphereSpl.VolumeJoin(context.Background(), actualHandle, volume, params.JoinArgs.MountPath, params.JoinArgs.Flags)
	if err != nil {
		log.Errorf("Volumes: StorageHandler : %#v", err)
		return storage.NewVolumeJoinInternalServerError().WithPayload(&models.Error{
			Code:    swag.Int64(http.StatusInternalServerError),
			Message: err.Error(),
		})
	}
	log.Infof("volume %s has been joined to a container", volume.ID)
	return storage.NewVolumeJoinOK().WithPayload(actualHandle.String())
}
Esempio n. 5
0
func (handler *ScopesHandlersImpl) ScopesUnbindContainer(params scopes.UnbindContainerParams) middleware.Responder {
	defer trace.End(trace.Begin("ScopesUnbindContainer"))

	h := exec.GetHandle(params.Handle)
	if h == nil {
		return scopes.NewUnbindContainerNotFound()
	}

	if err := handler.netCtx.UnbindContainer(h); err != nil {
		return scopes.NewUnbindContainerInternalServerError().WithPayload(errorPayload(err))
	}

	return scopes.NewUnbindContainerOK().WithPayload(h.String())
}
Esempio n. 6
0
func (handler *ScopesHandlersImpl) ScopesRemoveContainer(params scopes.RemoveContainerParams) middleware.Responder {
	defer trace.End(trace.Begin("ScopesRemoveContainer"))

	h := exec.GetHandle(params.Handle)
	if h == nil {
		return scopes.NewRemoveContainerNotFound().WithPayload(&models.Error{Message: "container not found"})
	}

	if err := handler.netCtx.RemoveContainer(h, params.Scope); err != nil {
		if _, ok := err.(*network.ResourceNotFoundError); ok {
			return scopes.NewRemoveContainerNotFound().WithPayload(errorPayload(err))
		}

		return scopes.NewRemoveContainerInternalServerError().WithPayload(errorPayload(err))
	}

	return scopes.NewRemoveContainerOK().WithPayload(h.String())
}
Esempio n. 7
0
func (handler *ScopesHandlersImpl) ScopesAddContainer(params scopes.AddContainerParams) middleware.Responder {
	defer trace.End(trace.Begin(fmt.Sprintf("handle(%s)", params.Config.Handle)))

	h := exec.GetHandle(params.Config.Handle)
	if h == nil {
		return scopes.NewAddContainerNotFound().WithPayload(&models.Error{Message: "container not found"})
	}

	err := func() error {
		var ip *net.IP
		if params.Config.NetworkConfig.Address != nil && *params.Config.NetworkConfig.Address != "" {
			i := net.ParseIP(*params.Config.NetworkConfig.Address)
			if i == nil {
				return fmt.Errorf("invalid ip address %q", *params.Config.NetworkConfig.Address)
			}

			ip = &i
		}

		if params.Config.NetworkConfig.Aliases != nil {
			log.Debugf("Links/Aliases: %#v", params.Config.NetworkConfig.Aliases)
		}

		options := &network.AddContainerOptions{
			Scope:   params.Config.NetworkConfig.NetworkName,
			IP:      ip,
			Aliases: params.Config.NetworkConfig.Aliases,
			Ports:   params.Config.NetworkConfig.Ports,
		}
		return handler.netCtx.AddContainer(h, options)
	}()

	if err != nil {
		if _, ok := err.(*network.ResourceNotFoundError); ok {
			return scopes.NewAddContainerNotFound().WithPayload(errorPayload(err))
		}

		return scopes.NewAddContainerInternalServerError().WithPayload(errorPayload(err))
	}

	return scopes.NewAddContainerOK().WithPayload(h.String())
}
Esempio n. 8
0
func (handler *ContainersHandlersImpl) CommitHandler(params containers.CommitParams) middleware.Responder {
	defer trace.End(trace.Begin(fmt.Sprintf("handle(%s)", params.Handle)))

	h := exec.GetHandle(params.Handle)
	if h == nil {
		return containers.NewCommitNotFound().WithPayload(&models.Error{Message: "container not found"})
	}

	if err := h.Commit(context.Background(), handler.handlerCtx.Session, params.WaitTime); err != nil {
		log.Errorf("CommitHandler error on handle(%s) for %s: %#v", h.String(), h.ExecConfig.ID, err)
		switch err := err.(type) {
		case exec.ConcurrentAccessError:
			return containers.NewCommitConflict().WithPayload(&models.Error{Message: err.Error()})
		default:
			return containers.NewCommitDefault(http.StatusServiceUnavailable).WithPayload(&models.Error{Message: err.Error()})
		}
	}

	return containers.NewCommitOK()
}
Esempio n. 9
0
func (handler *ScopesHandlersImpl) ScopesGetContainerEndpoints(params scopes.GetContainerEndpointsParams) middleware.Responder {
	defer trace.End(trace.Begin(params.HandleOrID))

	cid := params.HandleOrID
	// lookup by handle
	h := exec.GetHandle(cid)
	if h != nil {
		cid = h.ExecConfig.ID
	}

	c := handler.netCtx.Container(cid)
	if c == nil {
		return scopes.NewGetContainerEndpointsNotFound().WithPayload(errorPayload(fmt.Errorf("container not found")))
	}
	eps := c.Endpoints()
	ecs := make([]*models.EndpointConfig, len(eps))
	for i, e := range eps {
		ecs[i] = toEndpointConfig(e)
	}

	return scopes.NewGetContainerEndpointsOK().WithPayload(ecs)
}
Esempio n. 10
0
// StateChangeHandler changes the state of a container
func (handler *ContainersHandlersImpl) StateChangeHandler(params containers.StateChangeParams) middleware.Responder {
	defer trace.End(trace.Begin(fmt.Sprintf("handle(%s)", params.Handle)))

	h := exec.GetHandle(params.Handle)
	if h == nil {
		return containers.NewStateChangeNotFound()
	}

	var state exec.State
	switch params.State {
	case "RUNNING":
		state = exec.StateRunning
	case "STOPPED":
		state = exec.StateStopped
	case "CREATED":
		state = exec.StateCreated
	default:
		return containers.NewStateChangeDefault(http.StatusServiceUnavailable).WithPayload(&models.Error{Message: "unknown state"})
	}

	h.SetTargetState(state)
	return containers.NewStateChangeOK().WithPayload(h.String())
}