2018-08-27 19:10:55 +02:00
|
|
|
package job
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-03-20 23:01:24 +01:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2018-08-30 17:40:02 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
2018-09-23 23:04:31 +02:00
|
|
|
"github.com/zrepl/zrepl/config"
|
2018-08-27 19:10:55 +02:00
|
|
|
)
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
func JobsFromConfig(c *config.Config) ([]Job, error) {
|
2018-08-27 19:10:55 +02:00
|
|
|
js := make([]Job, len(c.Jobs))
|
|
|
|
for i := range c.Jobs {
|
|
|
|
j, err := buildJob(c.Global, c.Jobs[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-18 16:18:53 +02:00
|
|
|
if j == nil || j.Name() == "" {
|
|
|
|
panic(fmt.Sprintf("implementation error: job builder returned nil job type %T", c.Jobs[i].Ret))
|
|
|
|
}
|
2018-08-27 19:10:55 +02:00
|
|
|
js[i] = j
|
|
|
|
}
|
2019-03-20 23:01:24 +01:00
|
|
|
|
|
|
|
// receiving-side root filesystems must not overlap
|
|
|
|
{
|
|
|
|
rfss := make([]string, len(js))
|
|
|
|
for i, j := range js {
|
|
|
|
jrfs, ok := j.OwnedDatasetSubtreeRoot()
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rfss[i] = jrfs.ToString()
|
|
|
|
}
|
|
|
|
if err := validateReceivingSidesDoNotOverlap(rfss); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 19:10:55 +02:00
|
|
|
return js, nil
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:50:59 +02:00
|
|
|
func buildJob(c *config.Global, in config.JobEnum) (j Job, err error) {
|
2018-09-23 23:04:31 +02:00
|
|
|
cannotBuildJob := func(e error, name string) (Job, error) {
|
2018-10-18 16:18:53 +02:00
|
|
|
return nil, errors.Wrapf(e, "cannot build job %q", name)
|
2018-09-23 23:04:31 +02:00
|
|
|
}
|
2018-09-24 12:31:29 +02:00
|
|
|
// FIXME prettify this
|
2018-08-27 19:10:55 +02:00
|
|
|
switch v := in.Ret.(type) {
|
2018-08-27 22:21:45 +02:00
|
|
|
case *config.SinkJob:
|
2018-09-23 23:04:31 +02:00
|
|
|
m, err := modeSinkFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = passiveSideFromConfig(c, &v.PassiveJob, m)
|
2018-08-30 17:40:02 +02:00
|
|
|
if err != nil {
|
2018-09-23 23:04:31 +02:00
|
|
|
return cannotBuildJob(err, v.Name)
|
2018-08-30 17:40:02 +02:00
|
|
|
}
|
2018-09-24 12:31:29 +02:00
|
|
|
case *config.SourceJob:
|
|
|
|
m, err := modeSourceFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = passiveSideFromConfig(c, &v.PassiveJob, m)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
2018-11-20 19:30:15 +01:00
|
|
|
case *config.SnapJob:
|
2018-11-21 14:37:03 +01:00
|
|
|
j, err = snapJobFromConfig(c, v)
|
2018-11-20 19:30:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
2018-08-27 22:21:45 +02:00
|
|
|
case *config.PushJob:
|
2018-09-23 23:04:31 +02:00
|
|
|
m, err := modePushFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = activeSide(c, &v.ActiveJob, m)
|
2018-08-30 17:40:02 +02:00
|
|
|
if err != nil {
|
2018-09-23 23:04:31 +02:00
|
|
|
return cannotBuildJob(err, v.Name)
|
2018-08-30 17:40:02 +02:00
|
|
|
}
|
2018-09-24 12:31:29 +02:00
|
|
|
case *config.PullJob:
|
|
|
|
m, err := modePullFromConfig(c, v)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
|
|
|
j, err = activeSide(c, &v.ActiveJob, m)
|
|
|
|
if err != nil {
|
|
|
|
return cannotBuildJob(err, v.Name)
|
|
|
|
}
|
2018-08-27 19:10:55 +02:00
|
|
|
default:
|
2018-08-27 22:21:45 +02:00
|
|
|
panic(fmt.Sprintf("implementation error: unknown job type %T", v))
|
2018-08-27 19:10:55 +02:00
|
|
|
}
|
2018-09-23 23:04:31 +02:00
|
|
|
return j, nil
|
|
|
|
|
2018-08-27 22:21:45 +02:00
|
|
|
}
|
2019-03-20 23:01:24 +01:00
|
|
|
|
|
|
|
func validateReceivingSidesDoNotOverlap(receivingRootFSs []string) error {
|
|
|
|
if len(receivingRootFSs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
rfss := make([]string, len(receivingRootFSs))
|
|
|
|
copy(rfss, receivingRootFSs)
|
|
|
|
sort.Slice(rfss, func(i, j int) bool {
|
|
|
|
return strings.Compare(rfss[i], rfss[j]) == -1
|
|
|
|
})
|
|
|
|
// idea:
|
|
|
|
// no path in rfss must be prefix of another
|
|
|
|
//
|
|
|
|
// rfss is now lexicographically sorted, which means that
|
|
|
|
// if i is prefix of j, i < j (in lexicographical order)
|
|
|
|
// thus,
|
|
|
|
// if any i is prefix of i+n (n >= 1), there is overlap
|
|
|
|
for i := 0; i < len(rfss)-1; i++ {
|
|
|
|
if strings.HasPrefix(rfss[i+1], rfss[i]) {
|
|
|
|
return fmt.Errorf("receiving jobs with overlapping root filesystems are forbidden")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|