2020-06-25 03:25:51 +00:00
|
|
|
package edgejobs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
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"
|
2023-02-28 15:14:09 +00:00
|
|
|
"github.com/portainer/portainer/api/dataservices"
|
2022-12-19 21:54:51 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/edge"
|
|
|
|
"github.com/portainer/portainer/api/internal/slices"
|
2020-06-25 03:25:51 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id EdgeJobTasksCollect
|
|
|
|
// @summary Collect the log for a specifc task on an EdgeJob
|
2021-10-11 23:12:08 +00:00
|
|
|
// @description **Access policy**: administrator
|
2021-02-23 03:21:39 +00:00
|
|
|
// @tags edge_jobs
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
|
|
|
// @produce json
|
|
|
|
// @param id path string true "EdgeJob Id"
|
|
|
|
// @param taskID path string true "Task Id"
|
|
|
|
// @success 204
|
|
|
|
// @failure 500
|
|
|
|
// @failure 400
|
2021-09-13 03:42:53 +00:00
|
|
|
// @failure 503 "Edge compute features are disabled"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @router /edge_jobs/{id}/tasks/{taskID}/logs [post]
|
2020-06-25 03:25:51 +00:00
|
|
|
func (handler *Handler) edgeJobTasksCollect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
edgeJobID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
taskID, err := request.RetrieveNumericRouteVariableValue(r, "taskID")
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid Task identifier route variable", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:14:09 +00:00
|
|
|
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
|
|
|
edgeJob, err := tx.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID))
|
|
|
|
if handler.DataStore.IsErrObjectNotFound(err) {
|
|
|
|
return httperror.NotFound("Unable to find an Edge job with the specified identifier inside the database", err)
|
|
|
|
} else if err != nil {
|
|
|
|
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-02-28 15:14:09 +00:00
|
|
|
endpointID := portainer.EndpointID(taskID)
|
|
|
|
endpointsFromGroups, err := edge.GetEndpointsFromEdgeGroups(edgeJob.EdgeGroups, tx)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to get Endpoints from EdgeGroups", err)
|
|
|
|
}
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2023-02-28 15:14:09 +00:00
|
|
|
if slices.Contains(endpointsFromGroups, endpointID) {
|
|
|
|
edgeJob.GroupLogsCollection[endpointID] = portainer.EdgeJobEndpointMeta{
|
|
|
|
CollectLogs: true,
|
|
|
|
LogsStatus: portainer.EdgeJobLogsStatusPending,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
meta := edgeJob.Endpoints[endpointID]
|
|
|
|
meta.CollectLogs = true
|
|
|
|
meta.LogsStatus = portainer.EdgeJobLogsStatusPending
|
|
|
|
edgeJob.Endpoints[endpointID] = meta
|
2022-12-19 21:54:51 +00:00
|
|
|
}
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2023-02-28 15:14:09 +00:00
|
|
|
err = tx.EdgeJob().UpdateEdgeJob(edgeJob.ID, edgeJob)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to persist Edge job changes in the database", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := tx.Endpoint().Endpoint(endpointID)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to retrieve environment from the database", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.Edge.AsyncMode {
|
|
|
|
return httperror.BadRequest("Async Edge Endpoints are not supported in Portainer CE", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.ReverseTunnelService.AddEdgeJob(endpoint, edgeJob)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2023-01-17 17:21:13 +00:00
|
|
|
if err != nil {
|
2023-02-28 15:14:09 +00:00
|
|
|
if httpErr, ok := err.(*httperror.HandlerError); ok {
|
|
|
|
return httpErr
|
|
|
|
}
|
2023-01-17 17:21:13 +00:00
|
|
|
|
2023-02-28 15:14:09 +00:00
|
|
|
return httperror.InternalServerError("Unexpected error", err)
|
2023-01-17 17:21:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 03:25:51 +00:00
|
|
|
return response.Empty(w)
|
|
|
|
}
|