2022-02-18 22:36:19 +00:00
|
|
|
package main
|
|
|
|
|
2022-02-20 06:09:23 +00:00
|
|
|
import (
|
2022-11-04 21:31:54 +00:00
|
|
|
"errors"
|
2022-02-20 06:09:23 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-03-29 05:53:49 +00:00
|
|
|
"os"
|
2022-11-04 21:37:32 +00:00
|
|
|
"os/exec"
|
2022-03-26 05:44:04 +00:00
|
|
|
"strings"
|
2022-02-20 06:09:23 +00:00
|
|
|
|
2022-03-29 05:53:49 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2022-02-20 06:09:23 +00:00
|
|
|
"github.com/hashicorp/hcl/v2/hclsimple"
|
2022-03-29 05:53:49 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/function"
|
2022-02-20 06:09:23 +00:00
|
|
|
)
|
|
|
|
|
2022-02-18 22:36:19 +00:00
|
|
|
var (
|
2022-02-23 00:55:41 +00:00
|
|
|
// version of restic-scheduler being run.
|
2022-11-04 21:31:54 +00:00
|
|
|
version = "dev"
|
|
|
|
ErrJobNotFound = errors.New("jobs not found")
|
2022-02-18 22:36:19 +00:00
|
|
|
)
|
|
|
|
|
2022-03-29 05:53:49 +00:00
|
|
|
func ParseConfig(path string) ([]Job, error) {
|
2022-03-26 05:44:04 +00:00
|
|
|
var config Config
|
|
|
|
|
2022-03-29 05:53:49 +00:00
|
|
|
ctx := hcl.EvalContext{
|
|
|
|
Variables: nil,
|
|
|
|
Functions: map[string]function.Function{
|
|
|
|
"env": function.New(&function.Spec{
|
|
|
|
Params: []function.Parameter{{
|
|
|
|
Name: "var",
|
|
|
|
Type: cty.String,
|
|
|
|
}},
|
|
|
|
VarParam: nil,
|
|
|
|
Type: function.StaticReturnType(cty.String),
|
|
|
|
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
|
|
|
return cty.StringVal(os.Getenv(args[0].AsString())), nil
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
"readfile": function.New(&function.Spec{
|
|
|
|
Params: []function.Parameter{{
|
|
|
|
Name: "path",
|
|
|
|
Type: cty.String,
|
|
|
|
}},
|
|
|
|
VarParam: nil,
|
|
|
|
Type: function.StaticReturnType(cty.String),
|
|
|
|
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
|
|
|
content, err := os.ReadFile(args[0].AsString())
|
|
|
|
if err != nil {
|
|
|
|
return cty.StringVal(""), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cty.StringVal(string(content)), nil
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hclsimple.DecodeFile(path, &ctx, &config); err != nil {
|
2022-03-26 05:44:04 +00:00
|
|
|
return nil, fmt.Errorf("%s: Failed to decode file: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.Jobs) == 0 {
|
|
|
|
log.Printf("%s: No jobs defined in file", path)
|
|
|
|
|
|
|
|
return []Job{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, job := range config.Jobs {
|
|
|
|
if err := job.Validate(); err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: Invalid job: %w", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config.Jobs, nil
|
|
|
|
}
|
|
|
|
|
2022-03-29 05:53:49 +00:00
|
|
|
func ReadJobs(paths []string) ([]Job, error) {
|
2022-03-26 05:44:04 +00:00
|
|
|
allJobs := []Job{}
|
|
|
|
|
|
|
|
for _, path := range paths {
|
2022-03-29 05:53:49 +00:00
|
|
|
jobs, err := ParseConfig(path)
|
2022-03-26 05:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if jobs != nil {
|
|
|
|
allJobs = append(allJobs, jobs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 23:33:26 +00:00
|
|
|
if len(allJobs) == 0 {
|
|
|
|
return allJobs, fmt.Errorf("No jobs found in provided configuration: %w", ErrJobNotFound)
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
return allJobs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Set map[string]bool
|
|
|
|
|
2022-11-04 21:31:54 +00:00
|
|
|
func (s Set) Contains(key string) bool {
|
|
|
|
_, contains := s[key]
|
|
|
|
|
|
|
|
return contains
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
func NewSetFrom(l []string) Set {
|
|
|
|
s := make(Set)
|
|
|
|
for _, l := range l {
|
|
|
|
s[l] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-11-04 22:18:50 +00:00
|
|
|
// FilterJobs filters a list of jobs by a list of names.
|
2022-11-04 21:31:54 +00:00
|
|
|
func FilterJobs(jobs []Job, names []string) ([]Job, error) {
|
2022-03-26 05:44:04 +00:00
|
|
|
nameSet := NewSetFrom(names)
|
2022-11-04 21:31:54 +00:00
|
|
|
if nameSet.Contains("all") {
|
|
|
|
return jobs, nil
|
|
|
|
}
|
2022-03-26 05:44:04 +00:00
|
|
|
|
2022-11-04 21:31:54 +00:00
|
|
|
filteredJobs := []Job{}
|
2022-11-04 21:37:32 +00:00
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
for _, job := range jobs {
|
2022-11-04 21:31:54 +00:00
|
|
|
if nameSet.Contains(job.Name) {
|
|
|
|
filteredJobs = append(filteredJobs, job)
|
|
|
|
|
|
|
|
delete(nameSet, job.Name)
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:31:54 +00:00
|
|
|
var err error
|
|
|
|
if len(nameSet) > 0 {
|
|
|
|
err = fmt.Errorf("%w: %v", ErrJobNotFound, nameSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredJobs, err
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
func runBackupJobs(jobs []Job, names string) error {
|
2022-11-16 17:52:29 +00:00
|
|
|
if names == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
namesSlice := strings.Split(names, ",")
|
|
|
|
|
2022-11-10 21:04:18 +00:00
|
|
|
if len(namesSlice) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
jobs, filterJobErr := FilterJobs(jobs, namesSlice)
|
2022-11-04 21:31:54 +00:00
|
|
|
for _, job := range jobs {
|
|
|
|
if err := job.RunBackup(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
return filterJobErr
|
2022-11-04 21:31:54 +00:00
|
|
|
}
|
2022-03-26 05:44:04 +00:00
|
|
|
|
2023-04-25 21:02:28 +00:00
|
|
|
func runRestoreJobs(jobs []Job, names string, snapshot string) error {
|
2022-11-16 17:52:29 +00:00
|
|
|
if names == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
namesSlice := strings.Split(names, ",")
|
|
|
|
|
2022-11-10 21:04:18 +00:00
|
|
|
if len(namesSlice) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
jobs, filterJobErr := FilterJobs(jobs, namesSlice)
|
2022-03-26 05:44:04 +00:00
|
|
|
for _, job := range jobs {
|
2023-04-25 21:02:28 +00:00
|
|
|
if err := job.RunRestore(snapshot); err != nil {
|
2022-11-04 21:31:54 +00:00
|
|
|
return err
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
return filterJobErr
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 23:33:26 +00:00
|
|
|
type Flags struct {
|
|
|
|
showVersion bool
|
|
|
|
backup string
|
|
|
|
restore string
|
2023-04-25 21:02:28 +00:00
|
|
|
restoreSnapshot string
|
2022-11-14 23:33:26 +00:00
|
|
|
once bool
|
|
|
|
healthCheckAddr string
|
|
|
|
metricsPushGateway string
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFlags() Flags {
|
|
|
|
flags := Flags{} //nolint:exhaustruct
|
|
|
|
flag.BoolVar(&flags.showVersion, "version", false, "Display the version and exit")
|
|
|
|
flag.StringVar(&flags.backup, "backup", "", "Run backup jobs now. Names are comma separated. `all` will run all.")
|
|
|
|
flag.StringVar(&flags.restore, "restore", "", "Run restore jobs now. Names are comma separated. `all` will run all.")
|
|
|
|
flag.BoolVar(&flags.once, "once", false, "Run jobs specified using -backup and -restore once and exit")
|
|
|
|
flag.StringVar(&flags.healthCheckAddr, "addr", "0.0.0.0:8080", "address to bind health check API")
|
|
|
|
flag.StringVar(&flags.metricsPushGateway, "push-gateway", "", "url of push gateway service for batch runs (optional)")
|
2022-03-26 05:44:04 +00:00
|
|
|
flag.StringVar(&JobBaseDir, "base-dir", JobBaseDir, "Base dir to create intermediate job files like SQL dumps.")
|
2023-04-25 21:02:28 +00:00
|
|
|
flag.StringVar(&flags.restoreSnapshot, "snapshot", "latest", "the snapshot to restore")
|
2022-02-18 22:36:19 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-11-14 23:33:26 +00:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2023-04-25 21:02:28 +00:00
|
|
|
func runSpecifiedJobs(jobs []Job, backupJobs, restoreJobs, snapshot string) error {
|
2022-11-14 23:33:26 +00:00
|
|
|
// Run specified backup jobs
|
|
|
|
if err := runBackupJobs(jobs, backupJobs); err != nil {
|
|
|
|
return fmt.Errorf("Failed running backup jobs: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run specified restore jobs
|
2023-04-25 21:02:28 +00:00
|
|
|
if err := runRestoreJobs(jobs, restoreJobs, snapshot); err != nil {
|
2022-11-14 23:33:26 +00:00
|
|
|
return fmt.Errorf("Failed running restore jobs: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func maybePushMetrics(metricsPushGateway string) error {
|
|
|
|
if metricsPushGateway != "" {
|
|
|
|
if err := Metrics.PushToGateway(metricsPushGateway); err != nil {
|
|
|
|
return fmt.Errorf("Failed pushing metrics after jobs run: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flags := readFlags()
|
|
|
|
|
2022-02-18 22:36:19 +00:00
|
|
|
// Print version if flag is provided
|
2022-11-14 23:33:26 +00:00
|
|
|
if flags.showVersion {
|
2022-02-18 22:36:19 +00:00
|
|
|
fmt.Println("restic-scheduler version:", version)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2022-02-20 06:09:23 +00:00
|
|
|
|
2022-11-04 21:37:32 +00:00
|
|
|
if _, err := exec.LookPath("restic"); err != nil {
|
|
|
|
log.Fatalf("Could not find restic in path. Make sure it's installed")
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
if flag.NArg() == 0 {
|
2022-02-23 00:39:01 +00:00
|
|
|
log.Fatalf("Requires a path to a job file, but found none")
|
|
|
|
}
|
|
|
|
|
2022-03-29 05:53:49 +00:00
|
|
|
jobs, err := ReadJobs(flag.Args())
|
2022-03-26 05:44:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read jobs from files: %v", err)
|
2022-02-20 06:09:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 21:02:28 +00:00
|
|
|
if err := runSpecifiedJobs(jobs, flags.backup, flags.restore, flags.restoreSnapshot); err != nil {
|
2022-11-14 23:33:26 +00:00
|
|
|
log.Fatal(err)
|
2022-11-04 21:31:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
// Exit if only running once
|
2022-11-14 23:33:26 +00:00
|
|
|
if flags.once {
|
|
|
|
if err := maybePushMetrics(flags.metricsPushGateway); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-13 05:28:28 +00:00
|
|
|
go func() {
|
2022-11-14 23:33:26 +00:00
|
|
|
_ = RunHTTPHandlers(flags.healthCheckAddr)
|
2022-04-13 05:28:28 +00:00
|
|
|
}()
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
// TODO: Add healthcheck handler using Job.Healthy()
|
|
|
|
if err := ScheduleAndRunJobs(jobs); err != nil {
|
|
|
|
log.Fatalf("failed running jobs: %v", err)
|
2022-02-23 00:39:01 +00:00
|
|
|
}
|
2022-02-18 22:36:19 +00:00
|
|
|
}
|