2017-04-14 19:26:32 +02:00
|
|
|
package main
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-04-26 20:25:53 +02:00
|
|
|
"github.com/urfave/cli"
|
2017-04-29 18:26:43 +02:00
|
|
|
"github.com/zrepl/zrepl/jobrun"
|
2017-04-26 20:21:18 +02:00
|
|
|
"github.com/zrepl/zrepl/rpc"
|
2017-04-26 20:25:53 +02:00
|
|
|
"github.com/zrepl/zrepl/sshbytestream"
|
|
|
|
"io"
|
2017-04-29 18:26:43 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-04-26 20:21:18 +02:00
|
|
|
)
|
|
|
|
|
2017-04-14 19:26:32 +02:00
|
|
|
type Role uint
|
|
|
|
|
|
|
|
const (
|
2017-04-26 20:25:53 +02:00
|
|
|
ROLE_IPC Role = iota
|
2017-04-14 19:26:32 +02:00
|
|
|
ROLE_ACTION Role = iota
|
|
|
|
)
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
var conf Config
|
|
|
|
var handler Handler
|
2017-04-29 18:26:43 +02:00
|
|
|
var runner *jobrun.JobRunner
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
func main() {
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
app := cli.NewApp()
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
app.Name = "zrepl"
|
|
|
|
app.Usage = "replicate zfs datasets"
|
|
|
|
app.EnableBashCompletion = true
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{Name: "config"},
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
2017-04-26 20:25:53 +02:00
|
|
|
app.Before = func(c *cli.Context) (err error) {
|
2017-04-26 20:21:18 +02:00
|
|
|
if !c.GlobalIsSet("config") {
|
|
|
|
return errors.New("config flag not set")
|
|
|
|
}
|
|
|
|
if conf, err = ParseConfig(c.GlobalString("config")); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
handler = Handler{}
|
2017-04-29 18:26:43 +02:00
|
|
|
|
|
|
|
runner = jobrun.NewJobRunner()
|
2017-04-26 20:21:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
app.Commands = []cli.Command{
|
2017-04-26 20:25:53 +02:00
|
|
|
{
|
|
|
|
Name: "sink",
|
|
|
|
Aliases: []string{"s"},
|
|
|
|
Usage: "start in sink mode",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{Name: "identity"},
|
|
|
|
},
|
|
|
|
Action: doSink,
|
2017-04-26 20:21:18 +02:00
|
|
|
},
|
2017-04-26 20:25:53 +02:00
|
|
|
{
|
|
|
|
Name: "run",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Usage: "do replication",
|
|
|
|
Action: doRun,
|
|
|
|
},
|
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
app.RunAndExitOnError()
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
}
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
func doSink(c *cli.Context) (err error) {
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
var sshByteStream io.ReadWriteCloser
|
|
|
|
if sshByteStream, err = sshbytestream.Incoming(); err != nil {
|
|
|
|
return
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
return rpc.ListenByteStreamRPC(sshByteStream, handler)
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
func doRun(c *cli.Context) error {
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-29 18:26:43 +02:00
|
|
|
// Do every pull, do every push
|
|
|
|
// Scheduling
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
runner.Start()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := range conf.Pulls {
|
|
|
|
pull := conf.Pulls[i]
|
|
|
|
|
|
|
|
j := jobrun.Job{
|
|
|
|
Name: fmt.Sprintf("pull%d", i),
|
|
|
|
Interval: time.Duration(5 * time.Second),
|
|
|
|
Repeats: true,
|
|
|
|
RunFunc: func() error {
|
|
|
|
fmt.Printf("%v: %#v\n", time.Now(), pull)
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
fmt.Printf("%v: %#v\n", time.Now(), pull)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.AddJob(j)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range conf.Pushs {
|
|
|
|
push := conf.Pushs[i]
|
|
|
|
|
|
|
|
j := jobrun.Job{
|
|
|
|
Name: fmt.Sprintf("push%d", i),
|
|
|
|
Interval: time.Duration(5 * time.Second),
|
|
|
|
Repeats: true,
|
|
|
|
RunFunc: func() error {
|
|
|
|
fmt.Printf("%v: %#v\n", time.Now(), push)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.AddJob(j)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2017-04-14 19:26:32 +02:00
|
|
|
|
2017-04-26 20:21:18 +02:00
|
|
|
return nil
|
2017-04-14 19:26:32 +02:00
|
|
|
}
|