2018-11-05 20:58:15 +00:00
|
|
|
package schedules
|
|
|
|
|
|
|
|
import (
|
2018-11-14 03:10:49 +00:00
|
|
|
"errors"
|
2018-11-05 20:58:15 +00:00
|
|
|
"net/http"
|
2018-11-07 04:19:10 +00:00
|
|
|
"strconv"
|
2018-11-05 20:58:15 +00:00
|
|
|
|
2018-11-14 03:10:49 +00:00
|
|
|
"github.com/asaskevich/govalidator"
|
2018-11-05 20:58:15 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
|
|
|
"github.com/portainer/libhttp/response"
|
|
|
|
"github.com/portainer/portainer"
|
|
|
|
"github.com/portainer/portainer/cron"
|
|
|
|
)
|
|
|
|
|
|
|
|
type scheduleUpdatePayload struct {
|
|
|
|
Name *string
|
|
|
|
Image *string
|
|
|
|
CronExpression *string
|
|
|
|
Endpoints []portainer.EndpointID
|
2018-11-07 04:19:10 +00:00
|
|
|
FileContent *string
|
2018-11-09 02:22:08 +00:00
|
|
|
RetryCount *int
|
|
|
|
RetryInterval *int
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *scheduleUpdatePayload) Validate(r *http.Request) error {
|
2018-11-14 03:10:49 +00:00
|
|
|
if payload.Name != nil && !govalidator.Matches(*payload.Name, `^[a-zA-Z0-9][a-zA-Z0-9_.-]+$`) {
|
|
|
|
return errors.New("Invalid schedule name format. Allowed characters are: [a-zA-Z0-9_.-]")
|
|
|
|
}
|
2018-11-05 20:58:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) scheduleUpdate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
scheduleID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid schedule identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload scheduleUpdatePayload
|
|
|
|
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
schedule, err := handler.ScheduleService.Schedule(portainer.ScheduleID(scheduleID))
|
|
|
|
if err == portainer.ErrObjectNotFound {
|
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find a schedule with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a schedule with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:49:48 +00:00
|
|
|
updateJobSchedule := updateSchedule(schedule, &payload)
|
2018-11-05 20:58:15 +00:00
|
|
|
|
2018-11-07 04:19:10 +00:00
|
|
|
if payload.FileContent != nil {
|
|
|
|
_, err := handler.FileService.StoreScheduledJobFileFromBytes(strconv.Itoa(scheduleID), []byte(*payload.FileContent))
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist script file changes on the filesystem", err}
|
|
|
|
}
|
|
|
|
updateJobSchedule = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateJobSchedule {
|
2018-11-06 09:49:48 +00:00
|
|
|
jobContext := cron.NewScriptExecutionJobContext(handler.JobService, handler.EndpointService, handler.FileService)
|
2018-11-13 01:39:26 +00:00
|
|
|
jobRunner := cron.NewScriptExecutionJobRunner(schedule, jobContext)
|
|
|
|
err := handler.JobScheduler.UpdateJobSchedule(jobRunner)
|
2018-11-05 20:58:15 +00:00
|
|
|
if err != nil {
|
2018-11-06 09:49:48 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update job scheduler", err}
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.ScheduleService.UpdateSchedule(portainer.ScheduleID(scheduleID), schedule)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist schedule changes inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, schedule)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSchedule(schedule *portainer.Schedule, payload *scheduleUpdatePayload) bool {
|
2018-11-06 09:49:48 +00:00
|
|
|
updateJobSchedule := false
|
2018-11-05 20:58:15 +00:00
|
|
|
|
|
|
|
if payload.Name != nil {
|
|
|
|
schedule.Name = *payload.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Endpoints != nil {
|
2018-11-06 09:49:48 +00:00
|
|
|
schedule.ScriptExecutionJob.Endpoints = payload.Endpoints
|
|
|
|
updateJobSchedule = true
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if payload.CronExpression != nil {
|
|
|
|
schedule.CronExpression = *payload.CronExpression
|
2018-11-06 09:49:48 +00:00
|
|
|
updateJobSchedule = true
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Image != nil {
|
2018-11-06 09:49:48 +00:00
|
|
|
schedule.ScriptExecutionJob.Image = *payload.Image
|
|
|
|
updateJobSchedule = true
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 02:22:08 +00:00
|
|
|
if payload.RetryCount != nil {
|
|
|
|
schedule.ScriptExecutionJob.RetryCount = *payload.RetryCount
|
|
|
|
updateJobSchedule = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.RetryInterval != nil {
|
|
|
|
schedule.ScriptExecutionJob.RetryInterval = *payload.RetryInterval
|
|
|
|
updateJobSchedule = true
|
|
|
|
}
|
|
|
|
|
2018-11-06 09:49:48 +00:00
|
|
|
return updateJobSchedule
|
2018-11-05 20:58:15 +00:00
|
|
|
}
|