2020-06-25 03:25:51 +00:00
|
|
|
package endpointedge
|
|
|
|
|
|
|
|
import (
|
2023-05-18 17:58:33 +00:00
|
|
|
"errors"
|
2020-06-25 03:25:51 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2023-05-18 17:58:33 +00:00
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
2022-04-08 14:27:38 +00:00
|
|
|
"github.com/portainer/portainer/api/http/middlewares"
|
2023-09-01 22:27:02 +00:00
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/request"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/response"
|
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
|
2023-04-04 04:59:34 +00:00
|
|
|
// @param id path int true "environment(endpoint) Id"
|
|
|
|
// @param jobID path int true "Job Id"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @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 {
|
2022-04-08 14:27:38 +00:00
|
|
|
endpoint, err := middlewares.FetchEndpoint(r)
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
2022-04-08 14:27:38 +00:00
|
|
|
return httperror.BadRequest("Unable to find an environment on request context", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.Forbidden("Permission denied to access environment", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
edgeJobID, err := request.RetrieveNumericRouteVariableValue(r, "jobID")
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid edge job identifier route variable", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload logsPayload
|
|
|
|
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid request payload", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-05 23:27:20 +00:00
|
|
|
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
|
|
|
return handler.getEdgeJobLobs(tx, endpoint.ID, portainer.EdgeJobID(edgeJobID), payload)
|
|
|
|
})
|
2023-05-18 17:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
var httpErr *httperror.HandlerError
|
|
|
|
if errors.As(err, &httpErr) {
|
|
|
|
return httpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperror.InternalServerError("Unexpected error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) getEdgeJobLobs(tx dataservices.DataStoreTx, endpointID portainer.EndpointID, edgeJobID portainer.EdgeJobID, payload logsPayload) error {
|
|
|
|
endpoint, err := tx.Endpoint().Endpoint(endpointID)
|
|
|
|
if tx.IsErrObjectNotFound(err) {
|
|
|
|
return httperror.NotFound("Unable to find an environment with the specified identifier inside the database", err)
|
|
|
|
} else if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to find an environment with the specified identifier inside the database", err)
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:28:07 +00:00
|
|
|
edgeJob, err := tx.EdgeJob().Read(portainer.EdgeJobID(edgeJobID))
|
2023-05-18 17:58:33 +00:00
|
|
|
if tx.IsErrObjectNotFound(err) {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.NotFound("Unable to find an edge job with the specified identifier inside the database", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
} else if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to find an edge job with the specified identifier inside the database", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 17:58:33 +00:00
|
|
|
err = handler.FileService.StoreEdgeJobTaskLogFileFromBytes(strconv.Itoa(int(edgeJobID)), strconv.Itoa(int(endpointID)), []byte(payload.FileContent))
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to save task log to the filesystem", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
meta := portainer.EdgeJobEndpointMeta{CollectLogs: false, LogsStatus: portainer.EdgeJobLogsStatusCollected}
|
|
|
|
if _, ok := edgeJob.GroupLogsCollection[endpoint.ID]; ok {
|
|
|
|
edgeJob.GroupLogsCollection[endpoint.ID] = meta
|
|
|
|
} else {
|
|
|
|
edgeJob.Endpoints[endpoint.ID] = meta
|
|
|
|
}
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2023-06-22 21:28:07 +00:00
|
|
|
err = tx.EdgeJob().Update(edgeJob.ID, edgeJob)
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2023-01-26 14:32:11 +00:00
|
|
|
handler.ReverseTunnelService.AddEdgeJob(endpoint, edgeJob)
|
2020-06-25 03:25:51 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to persist edge job changes to the database", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 17:58:33 +00:00
|
|
|
return nil
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|