2020-08-03 22:18:53 +00:00
|
|
|
package stacks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
|
2021-01-25 19:16:53 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-08-21 03:16:38 +00:00
|
|
|
httperrors "github.com/portainer/portainer/api/http/errors"
|
|
|
|
"github.com/portainer/portainer/api/http/security"
|
|
|
|
|
2020-08-03 22:18:53 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
|
|
|
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id StackStart
|
|
|
|
// @summary Starts a stopped Stack
|
|
|
|
// @description Starts a stopped Stack.
|
|
|
|
// @description **Access policy**: restricted
|
|
|
|
// @tags stacks
|
|
|
|
// @security jwt
|
|
|
|
// @param id path int true "Stack identifier"
|
|
|
|
// @success 200 {object} portainer.Stack "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 403 "Permission denied"
|
|
|
|
// @failure 404 " not found"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /stacks/{id}/start [post]
|
2020-08-03 22:18:53 +00:00
|
|
|
func (handler *Handler) stackStart(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
stackID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
2020-08-21 03:16:38 +00:00
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
|
|
|
|
}
|
|
|
|
|
2020-08-03 22:18:53 +00:00
|
|
|
stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID))
|
|
|
|
if err == bolterrors.ErrObjectNotFound {
|
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID)
|
|
|
|
if err == bolterrors.ErrObjectNotFound {
|
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
2020-08-21 03:16:38 +00:00
|
|
|
err = handler.requestBouncer.AuthorizedEndpointOperation(r, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access endpoint", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
access, err := handler.userCanAccessStack(securityContext, endpoint.ID, resourceControl)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to verify user authorizations to validate stack access", err}
|
|
|
|
}
|
|
|
|
if !access {
|
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Access denied to resource", httperrors.ErrResourceAccessDenied}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stack.Status == portainer.StackStatusActive {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Stack is already active", errors.New("Stack is already active")}
|
|
|
|
}
|
|
|
|
|
2020-08-03 22:18:53 +00:00
|
|
|
err = handler.startStack(stack, endpoint)
|
|
|
|
if err != nil {
|
2021-02-16 21:37:27 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to start stack", err}
|
2020-08-03 22:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stack.Status = portainer.StackStatusActive
|
|
|
|
err = handler.DataStore.Stack().UpdateStack(stack.ID, stack)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update stack status", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, stack)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) startStack(stack *portainer.Stack, endpoint *portainer.Endpoint) error {
|
|
|
|
switch stack.Type {
|
|
|
|
case portainer.DockerComposeStack:
|
|
|
|
return handler.ComposeStackManager.Up(stack, endpoint)
|
|
|
|
case portainer.DockerSwarmStack:
|
|
|
|
return handler.SwarmStackManager.Deploy(stack, true, endpoint)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|