2020-06-25 03:25:51 +00:00
|
|
|
package endpointedge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-07 21:57:52 +00:00
|
|
|
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
2020-06-25 03:25:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type logsPayload struct {
|
|
|
|
FileContent string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *logsPayload) Validate(r *http.Request) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// endpointEdgeJobsLogs
|
|
|
|
// @summary Inspect an EdgeJob Log
|
2021-10-11 23:12:08 +00:00
|
|
|
// @description **Access policy**: public
|
2021-02-23 03:21:39 +00:00
|
|
|
// @tags edge, endpoints
|
|
|
|
// @accept json
|
|
|
|
// @produce json
|
2021-09-20 00:14:22 +00:00
|
|
|
// @param id path string true "environment(endpoint) Id"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @param jobID path string true "Job Id"
|
|
|
|
// @success 200
|
|
|
|
// @failure 500
|
|
|
|
// @failure 400
|
|
|
|
// @router /endpoints/{id}/edge/jobs/{jobID}/logs [post]
|
2020-06-25 03:25:51 +00:00
|
|
|
func (handler *Handler) endpointEdgeJobsLogs(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err}
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
2020-07-07 21:57:52 +00:00
|
|
|
if err == bolterrors.ErrObjectNotFound {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err}
|
2020-06-25 03:25:51 +00:00
|
|
|
} else if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an environment with the specified identifier inside the database", err}
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint)
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access environment", err}
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
edgeJobID, err := request.RetrieveNumericRouteVariableValue(r, "jobID")
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid edge job identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload logsPayload
|
|
|
|
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID))
|
2020-07-07 21:57:52 +00:00
|
|
|
if err == bolterrors.ErrObjectNotFound {
|
2020-06-25 03:25:51 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an edge job with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an edge job with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.FileService.StoreEdgeJobTaskLogFileFromBytes(strconv.Itoa(edgeJobID), strconv.Itoa(endpointID), []byte(payload.FileContent))
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to save task log to the filesystem", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := edgeJob.Endpoints[endpoint.ID]
|
|
|
|
meta.CollectLogs = false
|
|
|
|
meta.LogsStatus = portainer.EdgeJobLogsStatusCollected
|
|
|
|
edgeJob.Endpoints[endpoint.ID] = meta
|
|
|
|
|
|
|
|
err = handler.DataStore.EdgeJob().UpdateEdgeJob(edgeJob.ID, edgeJob)
|
|
|
|
|
|
|
|
handler.ReverseTunnelService.AddEdgeJob(endpoint.ID, edgeJob)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist edge job changes to the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, nil)
|
|
|
|
}
|