2023-07-02 02:43:55 -04:00
|
|
|
package schedulerdockerlocal
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
|
|
"github.com/dokku/dokku/plugins/common"
|
|
|
|
|
"github.com/dokku/dokku/plugins/cron"
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
|
|
base36 "github.com/multiformats/go-base36"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func deleteCrontab() error {
|
2024-02-13 01:09:24 -05:00
|
|
|
result, err := common.CallExecCommand(common.ExecCommandInput{
|
|
|
|
|
Command: "/usr/bin/crontab",
|
|
|
|
|
Args: []string{"-l", "-u", "dokku"},
|
|
|
|
|
Sudo: true,
|
|
|
|
|
})
|
2024-02-13 01:23:36 -05:00
|
|
|
if err != nil || result.ExitCode != 0 {
|
|
|
|
|
return nil
|
2023-07-02 02:43:55 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-13 01:09:24 -05:00
|
|
|
result, err = common.CallExecCommand(common.ExecCommandInput{
|
|
|
|
|
Command: "/usr/bin/crontab",
|
|
|
|
|
Args: []string{"-r", "-u", "dokku"},
|
|
|
|
|
Sudo: true,
|
|
|
|
|
})
|
2023-07-02 02:43:55 -04:00
|
|
|
if err != nil {
|
2024-02-13 01:09:24 -05:00
|
|
|
return fmt.Errorf("Unable to remove schedule file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if result.ExitCode != 0 {
|
|
|
|
|
return fmt.Errorf("Unable to remove schedule file: %s", result.StderrContents())
|
2023-07-02 02:43:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
common.LogInfo1("Removed")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generateCronEntries() ([]cron.TemplateCommand, error) {
|
|
|
|
|
apps, _ := common.UnfilteredDokkuApps()
|
|
|
|
|
|
|
|
|
|
g := new(errgroup.Group)
|
|
|
|
|
results := make(chan []cron.TemplateCommand, len(apps)+1)
|
|
|
|
|
for _, appName := range apps {
|
|
|
|
|
appName := appName
|
|
|
|
|
g.Go(func() error {
|
|
|
|
|
scheduler := common.GetAppScheduler(appName)
|
|
|
|
|
if scheduler != "docker-local" {
|
|
|
|
|
results <- []cron.TemplateCommand{}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c, err := cron.FetchCronEntries(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
results <- []cron.TemplateCommand{}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results <- c
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
|
commands := []cron.TemplateCommand{}
|
|
|
|
|
b, _ := common.PlugnTriggerOutput("cron-entries", "docker-local")
|
|
|
|
|
for _, line := range strings.Split(strings.TrimSpace(string(b[:])), "\n") {
|
|
|
|
|
if strings.TrimSpace(line) == "" {
|
|
|
|
|
results <- []cron.TemplateCommand{}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parts := strings.Split(line, ";")
|
|
|
|
|
if len(parts) != 2 && len(parts) != 3 {
|
|
|
|
|
results <- []cron.TemplateCommand{}
|
|
|
|
|
return fmt.Errorf("Invalid injected cron task: %v", line)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id := base36.EncodeToStringLc([]byte(strings.Join(parts, ";;;")))
|
|
|
|
|
command := cron.TemplateCommand{
|
|
|
|
|
ID: id,
|
|
|
|
|
Schedule: parts[0],
|
|
|
|
|
AltCommand: parts[1],
|
|
|
|
|
}
|
|
|
|
|
if len(parts) == 3 {
|
|
|
|
|
command.LogFile = parts[2]
|
|
|
|
|
}
|
|
|
|
|
commands = append(commands, command)
|
|
|
|
|
}
|
|
|
|
|
results <- commands
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
err := g.Wait()
|
|
|
|
|
close(results)
|
|
|
|
|
|
|
|
|
|
commands := []cron.TemplateCommand{}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return commands, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for result := range results {
|
|
|
|
|
c := result
|
|
|
|
|
if len(c) > 0 {
|
|
|
|
|
commands = append(commands, c...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return commands, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeCronEntries() error {
|
|
|
|
|
commands, err := generateCronEntries()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(commands) == 0 {
|
|
|
|
|
return deleteCrontab()
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-19 23:59:00 -04:00
|
|
|
mailto, _ := common.PlugnTriggerOutputAsString("cron-get-property", []string{"--global", "mailto"}...)
|
|
|
|
|
|
2023-07-02 02:43:55 -04:00
|
|
|
data := map[string]interface{}{
|
|
|
|
|
"Commands": commands,
|
2023-08-19 23:59:00 -04:00
|
|
|
"Mailto": mailto,
|
2023-07-02 02:43:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t, err := getCronTemplate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-22 01:59:22 +08:00
|
|
|
tmpFile, err := os.CreateTemp(os.TempDir(), fmt.Sprintf("dokku-%s-%s", common.MustGetEnv("DOKKU_PID"), "WriteCronEntries"))
|
2023-07-02 02:43:55 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Cannot create temporary schedule file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer tmpFile.Close()
|
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
|
|
|
|
|
if err := t.Execute(tmpFile, data); err != nil {
|
|
|
|
|
return fmt.Errorf("Unable to template out schedule file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 01:09:24 -05:00
|
|
|
result, err := common.CallExecCommand(common.ExecCommandInput{
|
|
|
|
|
Command: "/usr/bin/crontab",
|
|
|
|
|
Args: []string{"-u", "dokku", tmpFile.Name()},
|
|
|
|
|
Sudo: true,
|
|
|
|
|
})
|
2023-07-02 02:43:55 -04:00
|
|
|
if err != nil {
|
2024-02-13 01:09:24 -05:00
|
|
|
return fmt.Errorf("Unable to update schedule file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if result.ExitCode != 0 {
|
|
|
|
|
return fmt.Errorf("Unable to update schedule file: %s", result.StderrContents())
|
2023-07-02 02:43:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
common.LogInfo1("Updated schedule file")
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getCronTemplate() (*template.Template, error) {
|
|
|
|
|
t := template.New("cron")
|
|
|
|
|
|
|
|
|
|
templatePath := filepath.Join(common.MustGetEnv("PLUGIN_ENABLED_PATH"), "cron", "templates", "cron.tmpl")
|
2023-12-22 01:59:22 +08:00
|
|
|
b, err := os.ReadFile(templatePath)
|
2023-07-02 02:43:55 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return t, fmt.Errorf("Cannot read template file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s := strings.TrimSpace(string(b))
|
|
|
|
|
return t.Parse(s)
|
|
|
|
|
}
|