2020-06-25 03:25:51 +00:00
|
|
|
package edgejobs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
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"
|
2022-12-19 21:54:51 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/edge"
|
2022-09-28 17:56:32 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/endpointutils"
|
2022-12-19 21:54:51 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/maps"
|
2020-06-25 03:25:51 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id EdgeJobCreate
|
|
|
|
// @summary Create 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 method query string true "Creation Method" Enums(file, string)
|
2021-09-13 03:42:53 +00:00
|
|
|
// @param body_string body edgeJobCreateFromFileContentPayload true "EdgeGroup data when method is string"
|
|
|
|
// @param body_file body edgeJobCreateFromFilePayload true "EdgeGroup data when method is file"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @success 200 {object} portainer.EdgeGroup
|
2021-09-13 03:42:53 +00:00
|
|
|
// @failure 503 "Edge compute features are disabled"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @failure 500
|
|
|
|
// @router /edge_jobs [post]
|
2020-06-25 03:25:51 +00:00
|
|
|
func (handler *Handler) edgeJobCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
method, err := request.RetrieveQueryParameter(r, "method", false)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid query parameter: method. Valid values are: file or string", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch method {
|
|
|
|
case "string":
|
|
|
|
return handler.createEdgeJobFromFileContent(w, r)
|
|
|
|
case "file":
|
|
|
|
return handler.createEdgeJobFromFile(w, r)
|
|
|
|
default:
|
2022-12-19 21:54:51 +00:00
|
|
|
return httperror.BadRequest("Invalid query parameter: method. Valid values are: file or string", errors.New(strings.ToLower(request.ErrInvalidQueryParameter)))
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type edgeJobCreateFromFileContentPayload struct {
|
|
|
|
Name string
|
|
|
|
CronExpression string
|
|
|
|
Recurring bool
|
|
|
|
Endpoints []portainer.EndpointID
|
2022-12-19 21:54:51 +00:00
|
|
|
EdgeGroups []portainer.EdgeGroupID
|
2020-06-25 03:25:51 +00:00
|
|
|
FileContent string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *edgeJobCreateFromFileContentPayload) Validate(r *http.Request) error {
|
|
|
|
if govalidator.IsNull(payload.Name) {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid Edge job name")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !govalidator.Matches(payload.Name, `^[a-zA-Z0-9][a-zA-Z0-9_.-]*$`) {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid Edge job name format. Allowed characters are: [a-zA-Z0-9_.-]")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if govalidator.IsNull(payload.CronExpression) {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid cron expression")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
if (payload.Endpoints == nil || len(payload.Endpoints) == 0) && (payload.EdgeGroups == nil || len(payload.EdgeGroups) == 0) {
|
|
|
|
return errors.New("no environments or groups have been provided")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if govalidator.IsNull(payload.FileContent) {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid script file content")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createEdgeJobFromFileContent(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var payload edgeJobCreateFromFileContentPayload
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
edgeJob := handler.createEdgeJobObjectFromFileContentPayload(&payload)
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
var endpoints []portainer.EndpointID
|
|
|
|
if len(edgeJob.EdgeGroups) > 0 {
|
|
|
|
endpoints, err = edge.GetEndpointsFromEdgeGroups(payload.EdgeGroups, handler.DataStore)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to get Endpoints from EdgeGroups", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.addAndPersistEdgeJob(edgeJob, []byte(payload.FileContent), endpoints)
|
|
|
|
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to schedule Edge job", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, edgeJob)
|
|
|
|
}
|
|
|
|
|
|
|
|
type edgeJobCreateFromFilePayload struct {
|
|
|
|
Name string
|
|
|
|
CronExpression string
|
|
|
|
Recurring bool
|
|
|
|
Endpoints []portainer.EndpointID
|
2022-12-19 21:54:51 +00:00
|
|
|
EdgeGroups []portainer.EdgeGroupID
|
2020-06-25 03:25:51 +00:00
|
|
|
File []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *edgeJobCreateFromFilePayload) Validate(r *http.Request) error {
|
|
|
|
name, err := request.RetrieveMultiPartFormValue(r, "Name", false)
|
|
|
|
if err != nil {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid Edge job name")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !govalidator.Matches(name, `^[a-zA-Z0-9][a-zA-Z0-9_.-]+$`) {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid Edge job name format. Allowed characters are: [a-zA-Z0-9_.-]")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
payload.Name = name
|
|
|
|
|
|
|
|
cronExpression, err := request.RetrieveMultiPartFormValue(r, "CronExpression", false)
|
|
|
|
if err != nil {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid cron expression")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
payload.CronExpression = cronExpression
|
|
|
|
|
|
|
|
var endpoints []portainer.EndpointID
|
2022-12-19 21:54:51 +00:00
|
|
|
err = request.RetrieveMultiPartFormJSONValue(r, "Endpoints", &endpoints, true)
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid environments")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
payload.Endpoints = endpoints
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
var edgeGroups []portainer.EdgeGroupID
|
|
|
|
err = request.RetrieveMultiPartFormJSONValue(r, "EdgeGroups", &edgeGroups, true)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid edge groups")
|
|
|
|
}
|
|
|
|
payload.EdgeGroups = edgeGroups
|
|
|
|
|
|
|
|
if (payload.Endpoints == nil || len(payload.Endpoints) == 0) && (payload.EdgeGroups == nil || len(payload.EdgeGroups) == 0) {
|
|
|
|
return errors.New("no environments or groups have been provided")
|
|
|
|
}
|
|
|
|
|
2020-06-25 03:25:51 +00:00
|
|
|
file, _, err := request.RetrieveMultiPartFormFile(r, "file")
|
|
|
|
if err != nil {
|
2022-12-19 21:54:51 +00:00
|
|
|
return errors.New("invalid script file. Ensure that the file is uploaded correctly")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
payload.File = file
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createEdgeJobFromFile(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
payload := &edgeJobCreateFromFilePayload{}
|
|
|
|
err := payload.Validate(r)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
edgeJob := handler.createEdgeJobObjectFromFilePayload(payload)
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
var endpoints []portainer.EndpointID
|
|
|
|
if len(edgeJob.EdgeGroups) > 0 {
|
|
|
|
endpoints, err = edge.GetEndpointsFromEdgeGroups(payload.EdgeGroups, handler.DataStore)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to get Endpoints from EdgeGroups", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.addAndPersistEdgeJob(edgeJob, payload.File, endpoints)
|
|
|
|
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to schedule Edge job", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, edgeJob)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createEdgeJobObjectFromFilePayload(payload *edgeJobCreateFromFilePayload) *portainer.EdgeJob {
|
|
|
|
edgeJobIdentifier := portainer.EdgeJobID(handler.DataStore.EdgeJob().GetNextIdentifier())
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
endpoints := handler.convertEndpointsToMetaObject(payload.Endpoints)
|
2020-06-25 03:25:51 +00:00
|
|
|
|
|
|
|
edgeJob := &portainer.EdgeJob{
|
2022-12-19 21:54:51 +00:00
|
|
|
ID: edgeJobIdentifier,
|
|
|
|
Name: payload.Name,
|
|
|
|
CronExpression: payload.CronExpression,
|
|
|
|
Recurring: payload.Recurring,
|
|
|
|
Created: time.Now().Unix(),
|
|
|
|
Endpoints: endpoints,
|
|
|
|
EdgeGroups: payload.EdgeGroups,
|
|
|
|
Version: 1,
|
|
|
|
GroupLogsCollection: map[portainer.EndpointID]portainer.EdgeJobEndpointMeta{},
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return edgeJob
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) createEdgeJobObjectFromFileContentPayload(payload *edgeJobCreateFromFileContentPayload) *portainer.EdgeJob {
|
|
|
|
edgeJobIdentifier := portainer.EdgeJobID(handler.DataStore.EdgeJob().GetNextIdentifier())
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
endpoints := handler.convertEndpointsToMetaObject(payload.Endpoints)
|
2020-06-25 03:25:51 +00:00
|
|
|
|
|
|
|
edgeJob := &portainer.EdgeJob{
|
2022-12-19 21:54:51 +00:00
|
|
|
ID: edgeJobIdentifier,
|
|
|
|
Name: payload.Name,
|
|
|
|
CronExpression: payload.CronExpression,
|
|
|
|
Recurring: payload.Recurring,
|
|
|
|
Created: time.Now().Unix(),
|
|
|
|
Endpoints: endpoints,
|
|
|
|
EdgeGroups: payload.EdgeGroups,
|
|
|
|
Version: 1,
|
|
|
|
GroupLogsCollection: map[portainer.EndpointID]portainer.EdgeJobEndpointMeta{},
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return edgeJob
|
|
|
|
}
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
func (handler *Handler) addAndPersistEdgeJob(edgeJob *portainer.EdgeJob, file []byte, endpointsFromGroups []portainer.EndpointID) error {
|
2020-06-25 03:25:51 +00:00
|
|
|
edgeCronExpression := strings.Split(edgeJob.CronExpression, " ")
|
|
|
|
if len(edgeCronExpression) == 6 {
|
|
|
|
edgeCronExpression = edgeCronExpression[1:]
|
|
|
|
}
|
|
|
|
edgeJob.CronExpression = strings.Join(edgeCronExpression, " ")
|
|
|
|
|
|
|
|
for ID := range edgeJob.Endpoints {
|
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-28 17:56:32 +00:00
|
|
|
if !endpointutils.IsEdgeEndpoint(endpoint) {
|
2020-06-25 03:25:51 +00:00
|
|
|
delete(edgeJob.Endpoints, ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptPath, err := handler.FileService.StoreEdgeJobFileFromBytes(strconv.Itoa(int(edgeJob.ID)), file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
edgeJob.ScriptPath = scriptPath
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
var endpointsMap map[portainer.EndpointID]portainer.EdgeJobEndpointMeta
|
|
|
|
if len(endpointsFromGroups) > 0 {
|
|
|
|
endpointsMap = handler.convertEndpointsToMetaObject(endpointsFromGroups)
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
for ID := range endpointsMap {
|
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
if !endpointutils.IsEdgeEndpoint(endpoint) {
|
|
|
|
delete(endpointsMap, ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
maps.Copy(endpointsMap, edgeJob.Endpoints)
|
|
|
|
} else {
|
|
|
|
endpointsMap = edgeJob.Endpoints
|
|
|
|
}
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
if len(endpointsMap) == 0 {
|
|
|
|
return errors.New("environments or edge groups are mandatory for an Edge job")
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 21:54:51 +00:00
|
|
|
for endpointID := range endpointsMap {
|
|
|
|
handler.ReverseTunnelService.AddEdgeJob(endpointID, edgeJob)
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler.DataStore.EdgeJob().Create(edgeJob.ID, edgeJob)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|