1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-10 16:25:22 +02:00
portainer/api/http/handler/edgejobs/edgejob_tasklogs_clear.go
Prabhat Khera 6f5d9c357f
Some checks failed
/ triage (push) Has been cancelled
Lint / Run linters (push) Has been cancelled
Test / test-client (push) Has been cancelled
Test / test-server (push) Has been cancelled
Revert multiple commits to set it back to tag 2.19 (#10946)
* Revert "docs(dashboard): update link for swarm node [EE-6318] (#10770)"

This reverts commit 30356d2c15.

* Revert "docs(api): default to pascal case for property name [EE-6471] (#10861)"

This reverts commit 392819576c.

* Revert "fix(edge/jobs): clear logs [EE-5923] (#10819)"

This reverts commit e01386f63d.

* Revert "disable html5 validation (#10843)"

This reverts commit 4b0f08e92a.

* Revert "fix(edgestack): allow to set retry deployment  toggle EE-6167 (#10676) (#10805)"

This reverts commit eee632b22d.

* Revert "fix(stack): edit git stack validation (#10812)"

This reverts commit d3b150b29c.

* Revert "Revert "Revert "fix(rollback): reversed rollback code from 2.19.4 [EE-6435] (#10811)" (#10832)"

This reverts commit 32e05bb705.

* Revert "fix(setting/ssl): cert files are optional to upload [EE-6139] (#10779)"

This reverts commit 7408668dbb.

* Revert "fix(endpoint): delete the endpoint proxy when updating an endpoint address [EE-5577] (#10824)"

This reverts commit 4b5ea01456.

* Revert "fix(swagger): custom template create docs EE-6428 (#10806)"

This reverts commit 0d55cb3e08.

* Revert "fix(images): sort by tags [EE-6410] (#10755)"

This reverts commit 7f51c727a0.

* Revert "fix(stacks): sort by date [EE-5766] (#10758)"

This reverts commit 57b80cd9ac.

* Revert "fix(UI): remember backup settings tab [EE-6347] (#10764)"

This reverts commit c20452492d.

* Revert "fix(backup ui): minor typo on backup page EE-6348 (#10717)"

This reverts commit d58046eb68.

* Revert "fix(app): shift external to the top [EE-6392] (#10753) (#10802)"

This reverts commit 4795e85d18.

* Revert "fix(app): update sliders when limits are known [EE-5933] (#10769) (#10801)"

This reverts commit d090b0043a.

* Revert "fix(gitops): correct commit hash link [EE-6346] (#10800)"

This reverts commit 0e59cf76ec.

* Revert "fix toast error (#10804)"

This reverts commit 9978b88ed4.

* Revert "fix(kube): configmaps and secrets from envFrom in the app detail screen [EE-6282] (#10741) (#10798)"

This reverts commit c1a01558d0.

* Revert "fix(stacks): allow editing custom templates before stack deployment EE-6380 (#10713)"

This reverts commit f0aa0554f8.

* Reapply "fix(rollback): reversed rollback code from 2.19.4 [EE-6435] (#10811)

This reverts commit c58fa274e7.
2024-01-25 12:50:56 +13:00

126 lines
4.6 KiB
Go

package edgejobs
import (
"errors"
"net/http"
"strconv"
httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/dataservices"
"github.com/portainer/portainer/api/internal/edge"
"github.com/portainer/portainer/api/internal/slices"
"github.com/portainer/portainer/pkg/featureflags"
)
// @id EdgeJobTasksClear
// @summary Clear the log for a specifc task on an EdgeJob
// @description **Access policy**: administrator
// @tags edge_jobs
// @security ApiKeyAuth
// @security jwt
// @produce json
// @param id path int true "EdgeJob Id"
// @param taskID path int true "Task Id"
// @success 204
// @failure 500
// @failure 400
// @failure 503 "Edge compute features are disabled"
// @router /edge_jobs/{id}/tasks/{taskID}/logs [delete]
func (handler *Handler) edgeJobTasksClear(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
edgeJobID, err := request.RetrieveNumericRouteVariableValue(r, "id")
if err != nil {
return httperror.BadRequest("Invalid Edge job identifier route variable", err)
}
taskID, err := request.RetrieveNumericRouteVariableValue(r, "taskID")
if err != nil {
return httperror.BadRequest("Invalid Task identifier route variable", err)
}
mutationFn := func(edgeJob *portainer.EdgeJob, endpointID portainer.EndpointID, endpointsFromGroups []portainer.EndpointID) {
if slices.Contains(endpointsFromGroups, endpointID) {
edgeJob.GroupLogsCollection[endpointID] = portainer.EdgeJobEndpointMeta{
CollectLogs: false,
LogsStatus: portainer.EdgeJobLogsStatusIdle,
}
} else {
meta := edgeJob.Endpoints[endpointID]
meta.CollectLogs = false
meta.LogsStatus = portainer.EdgeJobLogsStatusIdle
edgeJob.Endpoints[endpointID] = meta
}
}
if featureflags.IsEnabled(portainer.FeatureNoTx) {
updateEdgeJobFn := func(edgeJob *portainer.EdgeJob, endpointID portainer.EndpointID, endpointsFromGroups []portainer.EndpointID) error {
return handler.DataStore.EdgeJob().UpdateEdgeJobFunc(edgeJob.ID, func(j *portainer.EdgeJob) {
mutationFn(j, endpointID, endpointsFromGroups)
})
}
err = handler.clearEdgeJobTaskLogs(handler.DataStore, portainer.EdgeJobID(edgeJobID), portainer.EndpointID(taskID), updateEdgeJobFn)
} else {
err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
updateEdgeJobFn := func(edgeJob *portainer.EdgeJob, endpointID portainer.EndpointID, endpointsFromGroups []portainer.EndpointID) error {
mutationFn(edgeJob, endpointID, endpointsFromGroups)
return tx.EdgeJob().Update(edgeJob.ID, edgeJob)
}
return handler.clearEdgeJobTaskLogs(tx, portainer.EdgeJobID(edgeJobID), portainer.EndpointID(taskID), updateEdgeJobFn)
})
}
if err != nil {
var handlerError *httperror.HandlerError
if errors.As(err, &handlerError) {
return handlerError
}
return httperror.InternalServerError("Unexpected error", err)
}
return response.Empty(w)
}
func (handler *Handler) clearEdgeJobTaskLogs(tx dataservices.DataStoreTx, edgeJobID portainer.EdgeJobID, endpointID portainer.EndpointID, updateEdgeJob func(*portainer.EdgeJob, portainer.EndpointID, []portainer.EndpointID) error) error {
edgeJob, err := tx.EdgeJob().Read(portainer.EdgeJobID(edgeJobID))
if tx.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)
}
err = handler.FileService.ClearEdgeJobTaskLogs(strconv.Itoa(int(edgeJobID)), strconv.Itoa(int(endpointID)))
if err != nil {
return httperror.InternalServerError("Unable to clear log file from disk", err)
}
endpointsFromGroups, err := edge.GetEndpointsFromEdgeGroups(edgeJob.EdgeGroups, tx)
if err != nil {
return httperror.InternalServerError("Unable to get Endpoints from EdgeGroups", err)
}
err = updateEdgeJob(edgeJob, endpointID, endpointsFromGroups)
if err != nil {
return httperror.InternalServerError("Unable to persist Edge job changes in the database", err)
}
err = handler.FileService.ClearEdgeJobTaskLogs(strconv.Itoa(int(edgeJobID)), strconv.Itoa(int(endpointID)))
if err != nil {
return httperror.InternalServerError("Unable to clear log file from disk", err)
}
endpoint, err := tx.Endpoint().Endpoint(endpointID)
if err != nil {
return httperror.NotFound("Unable to retrieve environment from the database", err)
}
handler.ReverseTunnelService.AddEdgeJob(endpoint, edgeJob)
return nil
}