mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
* feat(jobs): add job service interface * feat(jobs): create job execution api * style(jobs): remove comment * feat(jobs): add bindings * feat(jobs): validate payload different cases * refactor(jobs): rename endpointJob method * refactor(jobs): return original error * feat(jobs): pull image before creating container * feat(jobs): run jobs with sh * style(jobs): remove comment * refactor(jobs): change error names * feat(jobs): sync pull image * fix(jobs): close image reader after error check * style(jobs): remove comment and add docs * refactor(jobs): inline script command * fix(jobs): handle pul image error * refactor(jobs): handle image pull output * fix(docker): set http client timeout to 100s * feat(api): create schedule type * feat(agent): add basic schedule api * feat(schedules): add schedule service in bolt * feat(schedule): add schedule service to handler * feat(schedule): add and list schedules from db * feat(agent): get schedule from db * feat(schedule): update schedule in db * feat(agent): delete schedule * fix(bolt): remove sync method from scheduleService * feat(schedules): save/delete script in fs * feat(schedules): schedules cron service implementation * feat(schedule): integrate handler with cron * feat(schedules): schedules API overhaul * refactor(project): remove .idea folder * fix(schedules): fix script task execute call * refactor(schedules): refactor/fix golint issues * refactor(schedules): update SnapshotTask documentation * refactor(schedules): validate image name in ScheduleCreate operation
63 lines
1.9 KiB
Go
63 lines
1.9 KiB
Go
package cron
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/portainer/portainer"
|
|
)
|
|
|
|
// ScriptTaskContext represents the context required for the execution
|
|
// of a ScriptTask.
|
|
type ScriptTaskContext struct {
|
|
JobService portainer.JobService
|
|
EndpointService portainer.EndpointService
|
|
FileService portainer.FileService
|
|
ScheduleID portainer.ScheduleID
|
|
TargetEndpoints []portainer.EndpointID
|
|
}
|
|
|
|
// ScriptTask represents a task used to execute a script inside a privileged
|
|
// container. It can be scheduled.
|
|
type ScriptTask struct {
|
|
Image string
|
|
ScriptPath string
|
|
context *ScriptTaskContext
|
|
}
|
|
|
|
// NewScriptTask creates a new ScriptTask using the specified context.
|
|
func NewScriptTask(image, scriptPath string, context *ScriptTaskContext) ScriptTask {
|
|
return ScriptTask{
|
|
Image: image,
|
|
ScriptPath: scriptPath,
|
|
context: context,
|
|
}
|
|
}
|
|
|
|
// SetContext can be used to set/override the task context
|
|
func (task ScriptTask) SetContext(context *ScriptTaskContext) {
|
|
task.context = context
|
|
}
|
|
|
|
// Run triggers the execution of the task.
|
|
// It will iterate through all the endpoints specified in the context to
|
|
// execute the script associated to the task.
|
|
func (task ScriptTask) Run() {
|
|
scriptFile, err := task.context.FileService.GetFileContent(task.ScriptPath)
|
|
if err != nil {
|
|
log.Printf("scheduled task error (script execution). Unable to retrieve script file (err=%s)\n", err)
|
|
return
|
|
}
|
|
|
|
for _, endpointID := range task.context.TargetEndpoints {
|
|
endpoint, err := task.context.EndpointService.Endpoint(endpointID)
|
|
if err != nil {
|
|
log.Printf("scheduled task error (script execution). Unable to retrieve information about endpoint (id=%d) (err=%s)\n", endpointID, err)
|
|
return
|
|
}
|
|
|
|
err = task.context.JobService.Execute(endpoint, "", task.Image, scriptFile)
|
|
if err != nil {
|
|
log.Printf("scheduled task error (script execution). Unable to execute scrtip (endpoint=%s) (err=%s)\n", endpoint.Name, err)
|
|
}
|
|
}
|
|
}
|